Last active
January 3, 2016 12:48
-
-
Save kharmabum/8464821 to your computer and use it in GitHub Desktop.
Objective-C technical interview preparation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FTInterview.h | |
// Created by IO on 1/15/14. | |
// | |
// | |
#import <Foundation/Foundation.h> | |
#pragma mark - Type Definitions | |
typedef NS_ENUM(NSUInteger, FTInterviewPeriod) { | |
FTInterviewStart, | |
FTInterviewSuccess | |
}; | |
typedef NS_OPTIONS(NSUInteger, FTInterviewStatusOptions) { | |
FTInterviewStatusHappy = 0, | |
FTInterviewStatusHard = 1 << 0, | |
FTInterviewStatusEasy = 1 << 1 | |
}; | |
typedef void(^completionBlockType)(void); | |
typedef struct { | |
float a; | |
float b; | |
float c; | |
} customStruct; | |
#pragma mark - BFS/DFS | |
@interface FTNode : NSObject | |
@property (strong, nonatomic) NSSet *linkedNodes; | |
+ (void)breadthFirstSearch:(FTNode *)startingNode; | |
+ (void)depthFirstSearch:(FTNode *)startingNode; | |
@end | |
#pragma mark - FTInterview | |
@interface FTInterview : NSObject | |
@property (strong, nonatomic) NSNumber *value; | |
@property (readonly, assign, nonatomic) customStruct myStruct; | |
- (NSArray *)mergeSort:(NSArray *)unsortedArray; | |
@end | |
//protocol | |
@protocol FTInterviewDelegate <NSObject> | |
@optional | |
@end | |
// Category | |
@interface FTInterview (FTAdditions) | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FTInterview.m | |
// Pods | |
// | |
// Created by IO on 1/15/14. | |
// | |
// | |
#import "FTInterview.h" | |
typedef struct {int month; int day; int year;} Date; | |
static Date _today, _yesterday; | |
@implementation FTNode | |
+ (void)breadthFirstSearch:(FTNode *)startingNode | |
{ | |
NSMutableSet *visitedNodes = [NSMutableSet setWithObject:startingNode]; | |
NSMutableArray *queue = [NSMutableArray arrayWithObject:startingNode]; | |
while ([queue count]) { | |
NSSet *newNodes = ((FTNode *)[queue firstObject]).linkedNodes; | |
for (FTNode *newNode in newNodes) { | |
if (![visitedNodes containsObject:newNode]) { | |
[visitedNodes addObject:newNode]; | |
[queue addObject:newNode]; | |
} | |
} | |
[queue removeObjectAtIndex:0]; | |
} | |
} | |
+ (NSMutableSet *)recursiveSet | |
{ | |
static NSMutableSet *_recursiveSet; | |
if (!_recursiveSet) { | |
_recursiveSet = [[NSMutableSet alloc] init]; | |
} | |
return _recursiveSet; | |
} | |
+ (void)depthFirstSearch:(FTNode *)startingNode | |
{ | |
NSSet *newNodes = startingNode.linkedNodes; | |
for (FTNode *newNode in newNodes) { | |
if (![[self recursiveSet] containsObject:newNode]) { | |
[[self recursiveSet] addObject:newNode]; | |
[self depthFirstSearch:newNode]; | |
} | |
} | |
} | |
@end | |
#pragma mark - FTInterview | |
@interface FTInterview () | |
@property (readwrite, assign, nonatomic) customStruct myStruct; | |
@end | |
@implementation FTInterview | |
#pragma mark - Structs and NSValue | |
- (NSValue *)objectWithStruct | |
{ | |
Date date; date.month = 0; date.day = 0; date.year = 0; | |
NSValue *anObj = [NSValue value:&date withObjCType:@encode(Date)]; | |
return anObj; | |
// NSArray *array = [NSArray arrayWithObjects:anObj, nil]; | |
// Date dateAgain; | |
// [anObj getValue:&dateAgain]; | |
} | |
#pragma mark - Merge Sort | |
-(NSArray *)mergeSort:(NSArray *)unsortedArray | |
{ | |
NSUInteger middle = ([unsortedArray count]/2); | |
NSRange left = NSMakeRange(0, middle); | |
NSRange right = NSMakeRange(middle, ([unsortedArray count] - middle)); | |
NSArray *rightArr = [unsortedArray subarrayWithRange:right]; | |
NSArray *leftArr = [unsortedArray subarrayWithRange:left]; | |
return [self merge:[self mergeSort:leftArr] andRight:[self mergeSort:rightArr]]; | |
} | |
-(NSArray *)merge:(NSArray *)leftArr andRight:(NSArray *)rightArr | |
{ | |
NSMutableArray *result = [[NSMutableArray alloc] init]; | |
int right = 0; | |
int left = 0; | |
while (left < [leftArr count] && right < [rightArr count]) { | |
NSComparisonResult relation = [leftArr[left] compare:rightArr[right]]; | |
if (relation != NSOrderedDescending) { // equivalent to <= | |
[result addObject:[leftArr objectAtIndex:left++]]; | |
} | |
else { | |
[result addObject:[rightArr objectAtIndex:right++]]; | |
} | |
} | |
NSRange leftRange = NSMakeRange(left, ([leftArr count] - left)); | |
NSRange rightRange = NSMakeRange(right, ([rightArr count] - right)); | |
NSArray *newRight = [rightArr subarrayWithRange:rightRange]; | |
NSArray *newLeft = [leftArr subarrayWithRange:leftRange]; | |
newLeft = [result arrayByAddingObjectsFromArray:newLeft]; | |
return [newLeft arrayByAddingObjectsFromArray:newRight]; | |
} | |
#pragma mark - Comparisons | |
- (NSComparisonResult)compare:(NSNumber *)otherInterview { | |
return [self.value compare:otherInterview]; | |
} | |
//NSArray *sortedArray; | |
//sortedArray = [interviewArray sortedArrayUsingSelector:@selector(compare:)]; | |
- (NSArray *)compareWithSortDescription:(NSArray *)interviewArray | |
{ | |
NSSortDescriptor *sortDescriptor; | |
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" | |
ascending:YES]; | |
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; | |
NSArray *sortedArray; | |
sortedArray = [interviewArray sortedArrayUsingDescriptors:sortDescriptors]; | |
return sortedArray; | |
} | |
- (NSArray *)compareWithBlock:(NSArray *)interViewArray | |
{ | |
NSArray *sortedArray; | |
sortedArray = [interViewArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { | |
NSNumber *first = [(FTInterview *)a value]; | |
NSNumber *second = [(FTInterview *)b value]; | |
return [first compare:second]; | |
}]; | |
return sortedArray; | |
} | |
#pragma mark - NSObject | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
// | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment