Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Simple set intersection between the two people's friend lists will work here.
A while back I answered a similar but more general question: given two people in a social network, what is the shortest path of friend connections from one to the other? Discussed here: careercup DOT com/question?id=14087784
yes, its just intersection which takes O(n) where n is the number of friends each one has.
All nodes are white colour.
Mark each friend of A as black and while visiting the friends of B, if any one the friends is black, then he/she is a mutual friend of both. ---> O(n) where n is avg. number of friends one has.
Put the first user's list of friends in a hash table (with their user-id as key).
Iterate on the second user's list of friends, and lookup each of them in the hash table.
Any match is a common friend.
Time is O(M+N) where M and N are the number of friends of each user respecitively.
Space is O(min(M,N)) - the space for the hash table if we choose the user with less friends as the first user.
Do a BFS to get a path to second friend. Depending on whether you want a common friend who is directly connected to source and target friend, check the path length ( number of steps from Friend 1 to Friend 2). Any friend lying on the path would be a common friend if path length of more than 2 steps is allowed.
Innfriend is online platform to search your old school/college/company friends. Innfriend makes your search easy. Once you find your, innfriend helps you to get connected with your friend, you would keep getting update for any changes i.e. contact number, company, city, social networking profile etc. from your friends.
Find common elements in their adjacency list
- sonesh February 23, 2013