## Apple Interview Question for Staff Engineers

• 0

Country: United States

Comment hidden because of low score. Click to expand.
0
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
'''
Output:
[('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.

Comment hidden because of low score. Click to expand.
0
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])``````

Comment hidden because of low score. Click to expand.
0
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];
}];
}``````

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

``````return data.stream().
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
.collect(Collectors.toList());``````

Name:

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

### Books

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

### Videos

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.