Facebook | Phone | Friend Suggestion

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2020-01-20Added By Anonymous
Source: Link

Company: Meta (Facebook)
Role: e3, e4
Interview Stage: Phone Screen
Location: unknown
Year: 2020

Original Post Content:

This was from January 2020, and was asked in Facebook during a Phone Interview. 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 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