I have found an interesting iOS Developer Technical Interview Problem and solved it.
“I also have the ultimate iOS Developer technical test you can assign a potential hire. It should take 1-3 hours. It is easy to communicate, allows a lot of freedom of implementation so you can really get a better picture into how a developer thinks, and will make sure this developer knows the absolute fundamentals. Ready for it?
Calculate the and display each Fibonacci number from 1 -> max N possible on an iPhone with unsigned integers, and display each F(n) in a table view. The UITableView scrolling MUST remain smooth.
That’s it. You’ll be amazed at how profoundly simple this task sounds and yet how much iOS knowledge can be demonstrated. Not just what they know, but how they structure their work. You can assess their APIs, their separation of concerns when designing classes, the considerations they’ve made for performance, and their knowledge of concurrency. (Not to mention their knowledge of recursive functions.) It is ok to give them the formula, and allow them to use Google. F(n) = F(n-1) + F(n-2).”
GitHub repository with a solution is here.
I used recursion and concurrency. I added a little delay of 0.1 s to make a delay visible but scrolling is still smooth. This is my solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
// // FiboTableViewController.m // fibo // // Created by Dennis Kutlubaev on 05.11.14. // Copyright (c) 2014 Alwawee. All rights reserved. // #import "FiboTableViewController.h" @interface FiboTableViewController () { NSInteger _maxN; dispatch_queue_t _backgroundQueue; } @property (nonatomic, strong) NSMutableArray *tableData; @end @implementation FiboTableViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"Max Integer: %ld", NSIntegerMax); _maxN = 10000; _backgroundQueue = dispatch_queue_create("background queue", nil); [self loadData]; } - (void)loadData { _tableData = [NSMutableArray arrayWithArray:@[@(1),@(2)]]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _maxN; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; NSInteger n = indexPath.row; __block NSInteger fibo; if ( n < [_tableData count] ) { fibo = [_tableData[n] integerValue]; if (fibo > 0) { dispatch_async(dispatch_get_main_queue(), ^{ cell.textLabel.text = [NSString stringWithFormat:@"%ld", (long)fibo]; }); } } else { dispatch_async(_backgroundQueue, ^{ fibo = [self fibo:n]; dispatch_async(dispatch_get_main_queue(), ^{ if (fibo > 0) { cell.textLabel.text = [NSString stringWithFormat:@"%ld", (long)fibo]; } else { [self.tableView reloadData]; //cell.textLabel.text = [NSString stringWithFormat:@"Max integer reached: %ld", NSIntegerMax]; } }); }); } return cell; } //F(n) = F(n-1) + F(n-2) - (NSInteger)fibo:(NSInteger)n { NSInteger f; if ( n < [_tableData count] ) { f = [_tableData[n] integerValue]; } else { [NSThread sleepForTimeInterval:0.1]; f = [self fibo:(n-2)] + [self fibo:(n-1)]; if ((f < NSIntegerMax)&&(f > 0)) { [_tableData addObject:@(f)]; } else { f = 0; _maxN = n-1; } } return f; } @end |
It scrolls without lags on iPhone 6 Plus. I do not calculate twice a fibonacci number if already have calculated it, I store a new fibonacci number in a NSMutableArray.