Apple Interview Question for Staff Engineers

Country: United States

Comment hidden because of low score. Click to expand.
of 0 vote

Elegant 4 line solution in Python (for small strings of course):

def sortStrings(strs, col):
  data = [tuple(word.split()) for word in strs]
  data = [(first_name, last_name, int(age)) for first_name, last_name, age in data]
  result = sorted(data, key=lambda x: x[col-1])
  return result

Test Code:

strings = ['john doe 33', 'smith black 9', 'diana yale 12']
print(sortStrings(strings, 1)) # Order by first name
print(sortStrings(strings, 2)) # Order by last name
print(sortStrings(strings, 3)) # Order by age
[('diana', 'yale', 12), ('john', 'doe', 33), ('smith', 'black', 9)]
[('smith', 'black', 9), ('john', 'doe', 33), ('diana', 'yale', 12)]
[('smith', 'black', 9), ('diana', 'yale', 12), ('john', 'doe', 33)]

However, in a distributed setting, when we have large amounts of strings to process, we can divide up the column that we are trying to sort by and process per chunks. We would create multiple chunks and use a sorting algorithm such as quicksort or mergesort to combine the data into a sorted order.

- prudent_programmer March 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

If you want the output in the same string format as the input:

def sort_giant_string(giant, col, col_type=(str, str, int)):
  data = [row.split(" ") for row in giant.split("\n")]
  data.sort(key=lambda x: col_type[col - 1](x[col - 1]))
  return "\n".join([" ".join([str(cell) for cell in row]) for row in data])

- yehadut June 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

-(NSArray *) sortedArrayWithColumn:(NSUInteger) column {
    return [self sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2){
        NSArray *components1 = [obj1 componentsSeparatedByString:@","];
        NSArray *components2 = [obj2 componentsSeparatedByString:@","];
        if (column >= components1.count && column >= components2.count) {
            return NSOrderedSame;
        if (column >= components1.count) {
            return NSOrderedDescending;
        if (column >= components2.count) {
            return NSOrderedAscending;
        NSString *component1 = components1[column];
        NSString *component2 = components2[column];
        return [component1 caseInsensitiveCompare:component2];

- Rajiv Singh August 02, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

                filter(a -> !a.contains(s)) // s is the column heading so we exclude.
                .sorted(Comparator.comparing(k -> k.split(" ")[columnIndex]))  // sorted based on column index

- Anonymous June 04, 2021 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More