Mutual-Friend Recommendations
Reported by candidates from Meta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Meta reported this one in September 2026, and the constraints are the whole story. With n up to 100000 and 200000 edges, comparing user against every other user pair by pair is dead on arrival. The problem is friend-of-friend counting on an undirected graph. Build adjacency lists, walk two hops from the target user, count mutual friends per candidate, then sort by score descending and ID ascending. The follow-up about precision and recall at k is talk only. It doesn't change the return value. If you blank mid-assessment, StealthCoder is the silent backup that reads the problem and hands you the approach.
The problem
An undirected social graph contains n users numbered from 0 to n - 1. Every entry friendships[i] = [a, b] means that users a and b are direct friends. Recommend users for user according to these rules: Do not recommend user or anyone already directly connected to user. A remaining user is eligible only if they share at least one mutual friend with user. A candidate's score is the number of distinct mutual friends they share with user. Rank candidates by score from greatest to least. Break equal-score ties by user ID from least to greatest. Return the first k ranked user IDs, or every eligible candidate if fewer than k exist. Interview follow-up To evaluate this rule offline, remove a holdout set of known friendship edges, generate recommendations from the remaining graph, and measure precision at k and recall at k against those held-out connections. This evaluation discussion does not change the judged return value. Function recommendFriends(n: int, friendships: int[][], user: int, k: int) → int[] Examples Example 1 n = 6 friendships = [[0,1],[0,2],[1,3],[2,3],[1,4],[2,5]] user = 0 k = 3 return = [3,4,5] User 3 shares friends 1 and 2 with user 0, so its score is 2. Users 4 and 5 each score 1; their tie is ordered by ID. Example 2 n = 5 friendships = [[0,1],[0,2],[1,3],[2,3],[1,4],[2,4]] user = 0 k = 1 return = [3] Users 3 and 4 both have score 2. The ID tie-break ranks 3 first, and k = 1. Example 3 n = 5 friendships = [[0,1],[0,2],[1,2],[1,3],[2,4]] user = 0 k = 5 return = [3,4] Users 1 and 2 are already direct friends and remain excluded even though they are connected through each other. Candidates 3 and 4 each have one mutual friend. Example 4 n = 4 friendships = [[0,1],[2,3]] user = 0 k = 2 return = [] User 0 has no friend-of-friend candidate, so the result is empty. Constraints 1 <= n <= 100000. 0 <= friendships.length <= 200000. Every friendship contains two distinct valid user IDs. The graph has no duplicate friendship edges. 0 <= user < n. 0 <= k <= n.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to count from the user's side, not the candidate's side. Build adjacency lists. Put the user's direct friends in a set. For each friend f, loop over f's neighbors g. Skip g if it equals user or sits in the friend set. Otherwise increment a hash map count for g. Each friend contributes at most once per candidate because edges are unique, so the count is the distinct mutual friend count. Total work is bounded by the sum of friend degrees, which is at most 400000 edge visits. Then sort candidates by (-score, id) and slice the first k. Common pitfalls: forgetting to exclude the user, forgetting to exclude direct friends (Example 3 tests this), and returning wrong output when k is 0. Sorting is fine here. A heap only matters if you want to avoid sorting everything. StealthCoder is the hedge if the live OA rattles you and you lose the thread.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Mutual-Friend Recommendations cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Meta's OA.
Meta reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Mutual-Friend Recommendations FAQ
What's the trick in Meta's Mutual-Friend Recommendations problem?+
Don't compare pairs of users. Expand outward from the target user instead. For each direct friend, scan that friend's neighbors and tally a count per candidate. That tally is the mutual friend count. Then sort by score descending and ID ascending.
How hard is this really?+
It's medium. There's no exotic algorithm. It's adjacency lists, a hash set, a counting map, and a custom sort. Most failures come from missed exclusions or an O(n^2) approach that times out at n = 100000.
Do I need to implement the precision and recall evaluation?+
No. The follow-up about holding out edges and measuring precision at k and recall at k is discussion only. The problem says it doesn't change the judged return value. Just return the ranked list of user IDs.
What edge cases should I test before submitting?+
Test k = 0, which should return an empty list. Test a user with no friends, and a user whose friends only connect to each other like Example 3. Also check ties in score, where the lower ID must come first, and fewer eligible candidates than k.
How do I prepare for this in 48 hours?+
Write the solution once from scratch. Build adjacency lists, a friend set, a counting dictionary, and a sort with a key of negative score then ID. Run the four examples by hand. Then practice explaining why the complexity is bounded by the sum of friend degrees.