Find Mutual Friends From Two Sorted Friend ID Lists
Last updated: November 29, 2026
Quick Overview
Two users' friend lists are given as sorted arrays of user IDs. Return the sorted list of IDs that appear in both, each reported once.
Meta
November 29, 20260
0
0 solved
Two users' friend lists are given as sorted arrays of user IDs. Return the sorted list of IDs that appear in both, each reported once.
Mutual friends is a core Facebook feature, and the coding version tests whether you exploit sortedness with a linear merge rather than a hash set. Strong candidates also discuss what to do when one list is vastly larger than the other.
What the Interviewer Expects
- Uses a two-pointer merge for O(m + n) time and O(1) extra space
- Clarifies whether duplicates can appear and dedupes output
- Compares with the hash-set approach and when each wins
- Mentions binary search or galloping when sizes are skewed
- Handles empty lists
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- One user has 5 friends and the other has 5 million; what changes?
- How would you intersect k sorted lists?
- The lists live on two different machines; how do you minimize data transfer?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs are ascending lists of integers. Output is ascending, without duplicates. Expected O(m + n) time. Ask whether lists may contain repeats (a data bug, but worth guarding)....
High-Level Design
Advance two pointers like the merge step of merge sort. When the values are equal, emit the value if it differs from the last emitted one, then advance both. Otherwise advance the pointer at the small...