Social Meida Friend Recommendation
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's social media friend recommendation problem hit the assessment in August 2024, and it's a graph traversal question wrapped in a practical scenario. You're building a recommendation engine that suggests friends based on mutual connections. The core task is to find connections at a certain distance or identify clusters of people who should know each other. If you've seen BFS or DFS problems before, this one follows that playbook. StealthCoder will catch the edge cases you might miss if the wording trips you up during the live OA.
Pattern and pitfall
This is a graph problem disguised as a social network. You're likely given a user ID and need to return friend recommendations based on distance (friends of friends) or mutual connection count. The standard approach uses BFS to explore the graph level by level, tracking visited nodes to avoid cycles. A common pitfall is not handling disconnected components or forgetting to exclude the source node and already-connected users from recommendations. Some variants ask you to rank by mutual friend count, which adds a sorting step. The trick is usually about depth (how far to search) or filtering (which recommendations to actually return). If you blank on the exact algorithm during the OA, StealthCoder reads the problem statement and suggests the traversal pattern in real time.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Social Meida Friend Recommendation 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
This OA pattern shows up on LeetCode as number of connected components in an undirected graph. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google 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.
Social Meida Friend Recommendation FAQ
Is this a BFS or DFS problem?+
BFS is more natural here because you typically want recommendations by distance or degree of separation. It explores level by level, which maps cleanly to 'first-degree friends,' 'second-degree friends,' etc. DFS works but doesn't naturally give you distance ordering.
What if the graph is huge and has millions of users?+
The OA constraints will be reasonable, but the pattern is still valid. Real systems use ranking and caching. In the assessment, focus on correctness first, then optimize if you have time. BFS is efficient enough for the test.
Do I need to handle undirected vs. directed friendships?+
Most social networks treat friendship as mutual (undirected). But check the problem statement carefully. If it says 'follows' or 'subscribers,' it's directed. The algorithm is the same, just clarify the edge direction.
What's the trick with mutual friends?+
If the problem asks for 'mutual friend count,' you're intersecting the friend sets of the source user and each candidate. Use a set or hash table to count overlaps fast. This is a common follow-up or twist.
Should I exclude the source user and existing friends from recommendations?+
Yes. Always. Recommending someone their own ID or someone they already follow is nonsense. Filter both out before returning results.