Interview Question

IOS Developer Interview

-Toronto, ON

Booking.com

They asked a lot of iOS questions, and some general programming questions. The first question they asked was so obscure that I didn't even really understand it. I was probably dead from that point on. Another question was to figure out a way to combine three lists of items where an item would be placed in a destination array if it existed in any two of the source arrays.

AnswerAdd Tags

Interview Answers

6 Answers

1

For that, I first came up with a triple loop, but finally came over to using a hash table instead.

Anonymous on

2

$a = [5, 1, 2, 3, 4]; $b = [5, 6, 7, 3]; $c = []; $hash = []; //Hash the first array foreach($a as $v) { $hash[$v] = 1; } foreach($b as $v) { if ($hash[$v]) { $c[] = $v; } } print_r($c);

Anonymous on

0

public static void main(String[] args) { int a[] = {1, 2, 1, 4, 6, 7}; int b[] = {7, 6, 3}; int c[] = {5, 3, 4, 0, 9}; int maxLength = Math.max(Math.max(a.length, b.length), c.length); LinkedHashMap freq = new LinkedHashMap(); HashSet aSet = new HashSet(); HashSet bSet = new HashSet(); HashSet cSet = new HashSet(); for (int i = 0; i iterator = set.iterator(); while (iterator.hasNext()) { int key = iterator.next(); if (freq.get(key) >= 2) { System.out.print(key + ","); } } }

Mahdi on

0

I guess you were asked to find common elements within three arrays. I would give it a try to use NSCountedSet to count them then populate the result array with those common elements. NSArray *a = @[@1,@3,@8,@-9]; NSArray *b = @[@-1,@3,@0,@-9]; NSArray *c = @[@0,@31,@32,@8,@6]; -(NSArray*)find2ElementsAtleastPresentIn2Arrays:(NSArray*)aList:(NSArray*)bList:(NSArray*)cList{ NSMutableArray *resultArr = [[NSMutableArray alloc]init]; NSMutableArray *largeArray = [[NSMutableArray alloc]init]; [largeArray addObjectsFromArray:aList]; [largeArray addObjectsFromArray:bList]; [largeArray addObjectsFromArray:cList]; NSCountedSet *cs = [[NSCountedSet alloc]initWithArray:largeArray]; for(NSNumber *num in cs){ //NSLog(@"%d **** %@",[cs countForObject:num], num); if([cs countForObject:num]>1){ if(![resultArr containsObject:num]){ [resultArr addObject:num]; } } } NSLog(@"%@", resultArr); return resultArr; }

Baki on

0

I solved it using 2 for loops with time complexity O(n^2). Combine all three lists and check if each item exist 2 times then add to destination array otherwise ignore

Kiran on

0

@Kiran: You can't combine those three lists as they are not set so the items inside each of one of them could be repeated. For instance if you have few duplicated items in first list and combine them all, you would assume just because they are repeated they should be consider as the result. The rock solid is to compare list a to list b and have the result in a set and then a with c and b with c. The reason I keep the result in a set is to not have duplicate in the result as well. (Question didn't mention anything about this part so just a suggestion) Now you can work on hash table or other data structure to have a better time complexity as the question doesn't say anything about it.

John on

Add Answers or Comments

To comment on this, Sign In or Sign Up.