InterviewDB Experience

Friends Recommendation: Suggest New Friends Based on Mutual Connection Count

Interview Experience

Round 1 Coding

Problem

Given a social network as a list of friendships, recommend potential friends for a user. Recommendations are users who are not already friends with the target user but share the most mutual friends. Exclude the user themselves.

python
def recommend_friends(friendships: list[tuple[str,str]], user: str,
                       top_k: int) -> list[str]:

**returns** top_k recommended users sorted by mutual friend count desc,
    # ties broken alphabetically
    ...

Example

friendships = [
  ("Alice","Bob"),("Alice","Carol"),("Bob","David"),
  ("Carol","David"),("David","Eve"),("Alice","Eve")
]
recommend_friends(friendships, "Alice", top_k=2)
# Alice's friends: {Bob, Carol, Eve}
# David: shares Bob, Carol, Eve -> 2 mutuals (Bob,Carol) [Eve shares but Alice-Eve exists]
# Actually David shares Bob and Carol with Alice -> 2 mutuals
# -> ["David", ...]

Follow-ups

  1. What is the time complexity of your approach? Can you reduce it using adjacency sets?
  2. How would you handle a directed graph (follow vs. friend) instead of an undirected one?
  3. How would you extend recommendations to include second-degree connections with lower weight?
  4. At scale (100M users), how do you compute mutual friend counts efficiently?

Full Details

Round 1 Coding

Problem

Given a social network as a list of friendships, recommend potential friends for a user. Recommendations are users who are not already friends with the target user but share the most mutual friends. Exclude the user themselves.

python
def recommend_friends(friendships: list[tuple[str,str]], user: str,
                       top_k: int) -> list[str]:

**returns** top_k recommended users sorted by mutual friend count desc,
    # ties broken alphabetically
    ...

Example

friendships = [
  ("Alice","Bob"),("Alice","Carol"),("Bob","David"),
  ("Carol","David"),("David","Eve"),("Alice","Eve")
]
recommend_friends(friendships, "Alice", top_k=2)
# Alice's friends: {Bob, Carol, Eve}
# David: shares Bob, Carol, Eve -> 2 mutuals (Bob,Carol) [Eve shares but Alice-Eve exists]
# Actually David shares Bob and Carol with Alice -> 2 mutuals
# -> ["David", ...]

Follow-ups

  1. What is the time complexity of your approach? Can you reduce it using adjacency sets?
  2. How would you handle a directed graph (follow vs. friend) instead of an undirected one?
  3. How would you extend recommendations to include second-degree connections with lower weight?
  4. At scale (100M users), how do you compute mutual friend counts efficiently?

About This Question

This is a candidate experience report from a mongodb interview during the phone round.

It covers the following topics: Coding, Phone, Graph .