Last active
August 29, 2015 13:56
-
-
Save robotwholearned/9143783 to your computer and use it in GitHub Desktop.
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
| /*Problem Definition | |
| Imagine that you are a developer with the Lowe's iOS team. | |
| The team has been tasked with launching a new product suggestion service. | |
| The suggestion logic is based on the following rules: | |
| • A customer should only be suggested to purchase a product that they have not bought themselves. | |
| • A customer should only be suggested to purchase a product that their friends have purchased | |
| • The suggestion order should be driven by how many friends have purchased the same item | |
| •If multiple friends purchased the same item, it should be higher in the suggestions than a product that only one friend owns. | |
| You are provided two library functions to help you | |
| • getFriendsListForUser – returns a list of customer IDs (String) that identify the friends of a user | |
| • getPurchasesForUser – returns a list of product IDs (String) ordered by purchase date newest to oldest for a particular user | |
| For this evaluation, please: | |
| 1) Write a function that provides a ranked (high to low) list of product suggestions for a provided user. | |
| 2) Write code for a few key unit tests for your code, you may make assumptions about availability of mock data | |
| 3) Provide the space and time complexity of your solution. | |
| */ | |
| +(NSArray *) rankedProductSuggestions:(NSString *)userId | |
| { | |
| NSArray *rankedItmes= @[]; | |
| NSMutableDictionary *itemsAndRanks= [NSMutableDictionary new]; | |
| NSArray *friendsOfUser = [self getFriendsListForUser:userId]; | |
| NSMutableDictionary *friendsAndTheirPurchases = [NSMutableDictionary new]; | |
| NSMutableArray *listOfProducts; | |
| NSMutableDictionary *dict = [NSMutableDictionary new]; | |
| for (int i = 0; [friendsOfUser count] > i; i++) | |
| { | |
| NSString *userKey = friendsOfUser[i]; | |
| listOfProducts = [self getPurchasesForUser:userId]; | |
| NSMutableArray *friendsPurchases = [self getPurchasesForUser:userKey]; | |
| //remove perviously purchased items | |
| for(NSString *strings in listOfProducts) | |
| { | |
| [friendsPurchases removeObject:strings]; | |
| } | |
| [friendsAndTheirPurchases setObject:friendsPurchases forKey:userKey]; | |
| [dict setObject:friendsPurchases forKey:userKey]; | |
| } | |
| for (id key in friendsAndTheirPurchases) { | |
| id anObject = [friendsAndTheirPurchases objectForKey:key]; | |
| for (id second in anObject) | |
| { | |
| if (itemsAndRanks[second] == nil) | |
| {[itemsAndRanks setObject:@1 forKey:second];} | |
| else | |
| { | |
| [itemsAndRanks setObject:@([itemsAndRanks[second] intValue] + 1) forKey:second]; | |
| } | |
| } | |
| } | |
| rankedItmes = [itemsAndRanks keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){ | |
| int thing1 = [obj1 intValue]; | |
| int thing2 = [obj2 intValue]; | |
| if (thing1 > thing2) | |
| return NSOrderedAscending; | |
| else if (thing1 < thing2) | |
| return NSOrderedDescending; | |
| else | |
| return NSOrderedSame; | |
| }]; | |
| return rankedItmes; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment