Recommend friends-of-friends
Company: Meta
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: The question evaluates competency in graph traversal, set operations, and adjacency-list manipulation for social-network style data. Commonly asked in Coding & Algorithms interviews for Data Engineer roles, it gauges the ability to reason about relationship aggregation, duplicate elimination, and algorithmic efficiency when computing friends-of-friends, representing a practical application of algorithmic and data-structure knowledge rather than a purely conceptual exercise.
Constraints
- Each key in the graph maps to a (possibly empty) list of followees.
- A user may appear as a followee without being a key in the graph (they follow no one).
- Self-loops and mutual follows are possible and must be handled.
- The recommended set excludes the user themselves and anyone the user already directly follows.
- Results contain no duplicates.
Examples
Input: ({'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['E']}, 'A')
Expected Output: ['D', 'E']
Explanation: A follows B,C. B->C,D and C->E give candidates {C,D,E}; drop C (already followed) leaving D,E.
Input: ({'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['E']}, 'B')
Expected Output: ['E']
Explanation: B follows C,D. C->E and D has no followees; E is not already followed by B, so result is [E].
Input: ({'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['E']}, 'C')
Expected Output: []
Explanation: C follows E. E is not a key (follows no one), so there are no friends-of-friends.
Input: ({'A': ['B'], 'B': ['A']}, 'A')
Expected Output: []
Explanation: A follows B; B follows A. The only candidate is A itself, which is excluded.
Input: ({}, 'A')
Expected Output: []
Explanation: Empty graph: A follows no one, so there are no recommendations.
Input: ({'A': ['B', 'C'], 'B': ['D', 'E'], 'C': ['E', 'F']}, 'A')
Expected Output: ['D', 'E', 'F']
Explanation: Candidates {D,E,F} from B and C; E appears via both B and C but is deduped. None already followed by A.
Input: ({'X': []}, 'X')
Expected Output: []
Explanation: X follows nobody, so there are no friends-of-friends.
Hints
- First collect the set of users that `U` directly follows so membership checks are O(1).
- Iterate over each direct followee, then over the people that followee follows.
- Skip any candidate that equals `U` or is already in `U`'s direct-follow set; collect the rest in a set to dedupe.