Facebook | Phone | Friend Suggestion

Anonymous User
9424
Jan 20, 2020
Jan 20, 2020

This was from January 2020, and was asked in Facebook during a Phone Iterview. In addition, other friends of mine also reported to have seen this same question in Google as well.

Suppose you have a small, in-memory, social network and each person registered in it is modelled as follow:

Person: {int id, Set<Integer> friends}

Now code a method public Person friendSuggestion(Person p) which will return the person that has most in friends in common with the input 'p'. In case multiple people have the same number of friends in common, return any one of them. And in case there is no person to return, then NULL should be the answer.

Suppose the following scenario:

Person A: [B, C]
Person B: [A, D, E]
Person C: [A, D, F]
Person D: [B, C, E]
Person E: [B, D, F]
Person F: [C, E]

When we call friendSuggestion(A) it will return Person D.

Comments (8)