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).