Build a Friend Recommender
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in graph algorithms, recommendation system logic, input validation, metric design, and test-driven software implementation within the coding & algorithms domain for a machine learning engineering role, focusing on practical application of graph-based recommendation strategies.
Part 1: Filter Invalid Friend Recommendations
Constraints
- 0 <= len(friends), len(candidates) <= 100000
- User IDs are integers
- The friends list may be empty
- Candidates may contain duplicates
Examples
Input: (1, [2, 3], [1, 2, 4, 5])
Expected Output: [4, 5]
Explanation: 1 is the user themself and 2 is already a friend, so only 4 and 5 remain.
Input: (10, [20], [30, 30, 10, 20, 40])
Expected Output: [30, 40]
Explanation: 30 is valid but should appear once. 10 is self and 20 is already a friend.
Input: (7, [1, 2], [])
Expected Output: []
Explanation: Edge case: no candidates.
Input: (5, [], [5])
Expected Output: []
Explanation: Edge case: the only candidate is the user themself.
Hints
- Checking whether a candidate is already a friend should be O(1), not O(n).
- Use one structure to recognize current friends and another to avoid returning the same valid candidate twice.
Part 2: Seeded Random Valid Recommendation
Constraints
- 0 <= len(friends), len(candidates) <= 100000
- User IDs and seed are integers
- Candidates may contain duplicates
- The deterministic formula must be used exactly as stated
Examples
Input: (1, [2, 3], [1, 4, 5, 6], 0)
Expected Output: 5
Explanation: Valid list is [4, 5, 6]. Index = (0 * 31 + 7) % 3 = 1, so return 5.
Input: (10, [20], [30, 40, 30, 10, 20], 2)
Expected Output: 40
Explanation: Valid list is [30, 40]. Index = (2 * 31 + 7) % 2 = 1, so return 40.
Input: (5, [1, 2], [5, 1, 2], 99)
Expected Output: None
Explanation: Edge case: every candidate is invalid.
Input: (7, [], [7, 8, 8], 123)
Expected Output: 8
Explanation: Edge case: only one unique valid candidate remains.
Hints
- It is easier to pick a random item after you build the filtered valid list.
- Make sure the no-valid-candidate case is handled before taking a modulo.
Part 3: Compute a Graph-Only Recommendation Quality Score
Constraints
- 1 <= number of users in graph <= 10000
- 0 <= total number of friendship links <= 200000
- The graph is intended to be undirected
- Recommendation IDs may contain duplicates or IDs missing from the graph
Examples
Input: ({1: [2, 3], 2: [1, 3, 4], 3: [1, 2, 4], 4: [2, 3], 5: []}, 1, [4, 5, 2, 4, 1])
Expected Output: 2
Explanation: Valid unique recommendations are 4 and 5. User 1 shares 2 mutual friends with 4 and 0 with 5, so total score is 2.
Input: ({1: [2], 2: [1], 3: []}, 1, [])
Expected Output: 0
Explanation: Edge case: empty recommendation list.
Input: ({1: [2], 2: [1], 3: []}, 1, [1, 2, 4])
Expected Output: 0
Explanation: 1 is self, 2 is already a friend, and 4 is missing from the graph.
Input: ({1: [2, 3, 4], 2: [1, 5], 3: [1, 5], 4: [1, 5], 5: [2, 3, 4]}, 1, [5, 2, 5, 4, 1])
Expected Output: 3
Explanation: Only 5 is a unique valid recommendation. It shares mutual friends 2, 3, and 4 with user 1.
Hints
- The score of a single recommendation is based on the intersection size of two friend sets.
- Precomputing friend lists as sets makes repeated mutual-friend checks much faster.
Part 4: Rank Recommendations by Mutual Friends
Constraints
- 1 <= number of users in graph <= 10000
- 0 <= total number of friendship links <= 200000
- The graph is intended to be undirected
- User IDs are integers
Examples
Input: ({1: [2, 3], 2: [1, 4], 3: [1, 4], 4: [2, 3], 5: []}, 1)
Expected Output: [4, 5]
Explanation: User 4 has 2 mutual friends with user 1. User 5 has 0, so 4 comes first.
Input: ({1: [2], 2: [1, 3, 4], 3: [2], 4: [2], 5: []}, 1)
Expected Output: [3, 4, 5]
Explanation: Users 3 and 4 each have 1 mutual friend, so the smaller ID comes first. User 5 has 0.
Input: ({1: []}, 1)
Expected Output: []
Explanation: Edge case: a graph with a single user has no non-friends to recommend.
Input: ({1: [], 2: [], 3: [], 4: []}, 1)
Expected Output: [2, 3, 4]
Explanation: Edge case: all candidates have 0 mutual friends, so the order is by user ID.
Input: ({1: [2, 3], 2: [1, 3], 3: [1, 2]}, 1)
Expected Output: []
Explanation: Edge case: user 1 is already friends with everyone else.
Hints
- A candidate is anyone in the graph who is not the target user and not already a friend.
- Sort by a pair such as negative mutual count and then user ID to get the required order.