Solution for iOS Developer Technical Interview Problem

1 comment on Solution for iOS Developer Technical Interview Problem

Written by

in

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).”

This is the result:
IMG_0051

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:

//
//  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.

Comments

One response to “Solution for iOS Developer Technical Interview Problem”

  1. Kunal Balani Avatar
    Kunal Balani

    You don’t need an array just 4 pointers. 2 At top of table view and 2 at bottom views. When you scroll forward keep calculating values from bottom two pointers and vice versa for scrolling back.

Leave a Reply

Your email address will not be published. Required fields are marked *