The friend suggestion counted more than once
Repeated friendship edges inflate mutual-friend counts and move the wrong suggestion to the top.
What’s going wrong
Original Meta-style interview practice. This is not an actual Meta interview question. Bugger does not provide an AI assistant.
A friend recommender ranks candidates by mutual friends. Importing the same friendship twice, or in both directions, can change the ranking even though no new friendship was added. Repair graph construction so counts represent distinct people rather than input rows.
Input is {"user":"me","edges":[["me","ann"],["ann","zoe"],...],"limit":10}. Each edge is one undirected friendship, regardless of endpoint order. Duplicate and reversed edges describe the same friendship and must have no additional effect. Self-loop edges are ignored. Recommend only users who are not the target user or an existing direct friend and who share at least one distinct mutual friend with the target. A mutual friend is a distinct user adjacent to both the target and the candidate. Count each such person once per candidate.
Print an array of {"user":"zoe","mutualFriends":1} entries, sorted by descending mutualFriends and then ascending ASCII user ID, truncated to limit entries. Valid input has at most 300 edges. User IDs are nonempty ASCII strings up to 32 characters and match exactly. The target may not occur in any edge. Limit is an integer from 0 to 100. There are no weights, directed follows, privacy rules or external accounts. Repair buildGraph while preserving filtering and stable ranking.
Expected behavior
Treat friendship as an undirected set relation and rank nonfriends by their distinct mutual friends, with duplicate input, self-loops and reversed edges having no extra effect.
Reproduction
{"user":"me","edges":[["me","ann"],["ann","me"],["ann","zoe"],["zoe","ann"],["ann","zoe"],["me","ben"],["ben","zoe"],["ben","kai"]],"limit":10}[{"user":"zoe","mutualFriends":2},{"user":"kai","mutualFriends":1}]{"user":"me","edges":[["me","ann"],["ann","zoe"]],"limit":10}[{"user":"zoe","mutualFriends":1}]{"user":"me","edges":[["me","ann"],["me","ben"],["ann","ben"],["ann","x"]],"limit":10}[{"user":"x","mutualFriends":1}]Before you begin
- Undirected adjacency graphs
- Sets and maps
- Stable multi-key sorting
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.