Top Mutual-Friend Recommendations
Reported by candidates from Superhuman's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills the naive version of this Superhuman OA, reported in September 2026, is the candidate who is already a direct friend of the target. Skip that filter and you recommend people the target already knows. The problem is a friendship graph, a target user, and a ranking: mutual friend count descending, then name ascending, top three only. It's a hash-table counting problem with a sort at the end. If you blank on the setup, StealthCoder runs invisibly during the live OA as a safety net. Know the shape first and you probably won't need it.
The problem
You are given a simple undirected friendship graph as an array friends. Each entry contains the two user names joined by one friendship. You are also given a target user t. A recommendation candidate must not be t, must not already be a direct friend of t, and must share at least one direct friend with t. Rank candidates by these rules: A higher number of mutual friends comes first. When counts tie, the lexicographically smaller user name comes first. Return the first three user names in that order, or every candidate when fewer than three exist. Function recommendFriends(friends: String[][], t: String) → String[] Examples Example 1 friends = [["Alice","Bob"],["Alice","Carol"],["Alice","Dave"],["Bob","Carol"],["Bob","Eve"],["Bob","Frank"],["Carol","Dave"],["Carol","Grace"],["Dave","Grace"],["Dave","Henry"],["Eve","Frank"],["Eve","Grace"],["Frank","Grace"]] t = "Alice" return = ["Grace","Eve","Frank"] Grace shares two friends with Alice. Eve, Frank, and Henry each share one; lexical order selects Eve and Frank for the remaining positions. Example 2 friends = [["A","B"],["B","C"],["B","D"]] t = "A" return = ["C","D"] C and D each share B with A, so both are returned in lexical order. Constraints 2 <= friends.length <= 2 * 10^5 Every friends[i] contains exactly two user names. Every user name has length at most 10 and contains only Latin letters. The graph has no self-loops or duplicate undirected edges. The target user t appears in at least one friendship pair.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build an adjacency map from the edge list, using sets of names. Get t's direct friends. For each friend f of t, loop over f's neighbors. Skip t itself and skip anyone in t's friend set. Increment a counter for everyone else. That counter is the mutual friend count, and a candidate with zero mutuals never appears because you only reach people through a shared friend. Then sort by count descending and name ascending, and take the first three. The pitfall is the naive approach of comparing every user pair, which blows up with 2 * 10^5 edges. Walking only two hops from t keeps it near linear plus the sort. Another trap is string comparison: names are Latin letters only, so the default lexicographic sort works, but keep case as given. If you freeze on the live OA, StealthCoder can surface this two-hop counting approach so you can type it out.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Top 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Superhuman's OA.
Superhuman reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Top Mutual-Friend Recommendations FAQ
What's the trick in the Superhuman mutual-friend recommendation problem?+
Don't compare all pairs. Start at the target, visit each direct friend, then visit that friend's neighbors and count how often each person shows up. That count is the mutual friend total. Filter out the target and the target's direct friends, then sort.
How hard is this problem really?+
Medium-easy. There's no fancy algorithm. It's an adjacency map, a counter, and a custom sort. Most failures come from missing the filter for existing friends or from building the graph one-directionally when edges are undirected.
Do I need a heap for the top three?+
No. A full sort of the candidates is fine and simpler to get right. A heap or partial selection saves a little time, but sorting by negative count then name is less error-prone under pressure. Only optimize if you have spare minutes.
What edge cases should I test?+
Test a target whose friends have no other friends, so the result is empty. Test fewer than three candidates. Test ties on count that need name ordering. Test a candidate who is already a direct friend of the target and must be excluded.
How do I prepare for this in 48 hours?+
Write it once from scratch: build an undirected adjacency map, count two-hop neighbors with a dictionary, filter, then sort with a two-key comparator. Run both examples by hand. Practice the comparator in your language, since that's where small bugs hide.