iOS developer facebook面试问题

A few facebook iOS interview questions, 查阅了一些资料,整理了一下。

1.Views: A square subview is centered when viewed in landscape. When the device is rotated to portrait, the square ends up closer to the top-right corner. What’s wrong?

A:The frame of the subview is hard-coded. As in the landscape mode, the frame.x will be larger than frame.x in portrait mode if centered, the frame.y will be smaller than frame.y in portrait mode though.

2.Memory: What memory management policy do you use for an object’s delegate? Why?

Use assign in non-ARC, and weak reference in ARC mode. The object should not grab ownership of the delegate object. Its life cycle should not be determined by the delegate message sender object.

Example: A view that has a button on it, the button delegate its touch event to the view. It’s easy that we find both view and button hold strong reference if the delegate is strong reference. To avoid retain cycle, the delegate should be weak.

3.UX: When would you use a home-screen navigation or tab-bar navigation?

DashBoard: What I like: Good feature overview; a “hub” for all functions and information. What I don’t like: All menu items have more or less the same priority; if there are many items they don’t fit on one screen.

Tab-bar: The bar would always be there, the home screen would be the most important functionality of the app. What I like: No navigation needed if you want to use the main functionality; many apps use this approach so I guess users are quite familiar with this pattern. What I don’t like: You shouldn’t put more than 5 items on the bar so we would have a “more” button and (see this article: The iPhone Tab Bar)

A: no more than 5 items, use tab bar,put the most important at the home screen. A lot of items of the same priority, use home style navigation.

4.Understanding of platform: What is toll-free bridging?

A: So there are two framework : Core Foundation & Foundation. Core Foundation is a C language programming API set. Foundation is objective-C language programming API set. The type will be CF and NS. To use them in iOS, need to cast CF to NS if needed. Toll-free bridge is the feature to realize it.

Tip: __bridge_transfer will treat CF object as ARC. _bridge need to be released manually.

5.How will you describe iOS manual memory management for a new developer in few words?

Every object exists in memory must have a reference counter more than 0, retain the object reference will increase the counter by 1, assign will not change counter, but use the object reference, release will decrease the counter by 1, copy will copy the object in memory and retain the new allocated object reference. When the counter is 0 the object will be freed, any more references to the object will return nil.

6.How would you implement call for canceling queued blocks with dispatch_after?

A: first, grand central dispatch(GCD) is a low-level way of managing things. Anything currently running in the queue cannot be stopped.

dispatch_after will dispatch things after set-up delay time.

@interface Canceller : NSObject

@property (atomic, strong) BOOL shouldCancel;


@implementation Canceller;
//synthesize the shouldCancel, create ivar for it
@synthesize shouldCancel = _shouldCancel;


static void test(int a){
    static Canceller * canceller = nil;

        [canceller setShouldCancel:YES];
        [canceller release];
    canceller = [[Canceller alloc] init];
    dispatch_async(q,^ {
        while(![canceller shouldCancel]){NSLog(@"query %d",a);sleep(2);}


int main(int argc, const char* argv[]){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    //blah blah blah


    [pool release];
    return 0;

The above code should stop the test(1) and continue to run on test(2), notice that it is in non-ARC mode, will continue to write about cases in ARC mode.

The whole point is: if you are sure you need to stop the queue at some point, u need to give a boolean sign to the certain queued task, so you are able to check the boolean when really to execute the queue.

To be continued;

7. Find the kth point that is closest to the origin.

This question is basically asking: find the kth minimum value from an unsorted array.

The easiest solution is to use insertion sort, so we will find the minimum value in each round, until we find the kth minimum in the array. The time comlexity is O(kn).

A better solution can use quick select. The same rule as in quick sort, we randomly assign a pivot in the array, and scan the array, put the smaller element in the left of pivot and bigger in the right side. Unlike in the quick sort, since we only need to find the kth minimum, we dont need to check both sides. If there are x > k elements in the left, we only need to sort the left half, vice versa, only when the pivot reposition is the same as k, we can simply return the [array objectAtIndex:k] value.

Here is the objc code for the method:

#import <Foundation/Foundation.h>

@interface Solution : NSObject

- (int)find:(int)position InPoints:(NSArray *)array;


#import "Solution.h"

@implementation Solution

- (int)find:(int)position InPoints:(NSMutableArray *)array{
    return [self quickSelect:position-1 InPoints:array from:0 to:(int)[array count]-1];

- (int)quickSelect:(int)position InPoints:(NSMutableArray *)array from:(int) start to:(int)end{
    int result = [self partition:array from:start to:end];
    if(result == position)
        return [[array objectAtIndex:result] intValue];
    if(result > position){
        return [self quickSelect:position InPoints:array from:start to:result - 1];
        return  [self quickSelect:position InPoints:array from:result + 1 to:end];

- (int)partition:(NSMutableArray *)array from:(int)start to:(int)end{
    int pivot = [[array objectAtIndex:start] intValue];
    int low = start;
    int high = end;
    while(low < high){
        while(pivot > [[array objectAtIndex:low] intValue])
            low ++;
        while(pivot < [[array objectAtIndex:high] intValue])
            high --;
        if(low < high){
            NSNumber *temp = [array objectAtIndex:low];
            [array setObject:[array objectAtIndex:high] atIndexedSubscript:low];
            [array setObject:temp atIndexedSubscript:high];
            return low;
    return low;


This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s