Google | Phone Interview | Suggested Friend
Anonymous User
1354

I was rattled at this question so much I got stuck like deer in a headlights. I did get the solution but it was not nearly as fast/smooth as I knew I could have done it.

Given a UserA and his friendlist, return a suggested friend for the UserA. A suggested friend should be the friend of a friend(or n friends later) that have the most shared connections.
A- -B _______________________ Here we would return userD as he as two mutaul connections to userA,
| ~~ | _______________________ UserB and UserC are direct friends, and UserD is a shared frien between those two direct friends.
C- -D
| ____________________________ After grinding leetcode I was trying to overcomplicate this problem thinking recursion, memoization, etc. But it
E ___________________________ only needed to solve this case. So I got solution, create a User object(listOfFriends), walk thru the list add them to a map. Them go thru userAs friends BFS style, add them all to a map and update their 'count' if a mutual friend was found. Then get the max, and return that corresponding user.

I was expecting something much more uh idk different. Sadly this was easy, but it threw me for a loop and I stuttered and maybe dropped the ball on this. But hopefully I can help someone.
Best of Luck to all.

Comments (5)