Limited Time Offer:Up to 20% off Hello Interview Premium
Up to 20% off Hello Interview Premium 🎉
Hello Interview
Learn AI Coding
Introduction
Interview Formats
How to Prepare
Patterns
Codebase Orientation
Planning Your Approach
Driving the AI
Verification & Testing
Communication
Graph Search & Pathfinding
Topological Sort
Backtracking
Greedy & Bin Packing
Dynamic Programming
String Matching & Parsing
Data Structure Design
Battleship
Inventory Packer
Spell Checker
Card Game
Friend Recommender
Maze Solver
Route Planner
Task Scheduler
Word Container
Connect Four
Kitchen Orders
Maximize Unique Characters
Nonogram Solver
Pricing
Sign in / Sign up
Search
⌘K
Pricing

Tutor

Problem Breakdown

Friend Recommender

Graph Search & Pathfinding
Published ·
medium

Try This Problem Yourself

Practice in the AI-enabled editor with real-time feedback

You're given a social network, which is a collection of users and their bidirectional friendships. For a given user, return up to 5 recommended friends ranked by the number of mutual friends they share with the user. Ties break alphabetically by name. The user cannot be recommended to themselves, existing friends are excluded, and candidates must share at least one mutual friend.
Small social graph with Alice at the center, her three direct friends in the inner ring, and her three recommendations Eve, Frank, and Grace highlighted on the outer ring
Small social graph with Alice at the center, her three direct friends in the inner ring, and her three recommendations Eve, Frank, and Grace highlighted on the outer ring
The code is split across two classes. SocialNetwork owns the adjacency data and exposes helpers for fetching a user's friends, counting mutuals between two users, and checking whether a candidate is a valid recommendation. Recommender holds a reference to one SocialNetwork and implements a single recommend method that takes a user and a maximum result count. Every solution below lives inside that one method.

Solution 1, scan every user

The most natural solution is the literal restatement of the rules. You walk every user in the network, skip the invalid ones, count mutuals for each valid one, sort by those counts, and take the top five. The validity helper already combines the three exclusion rules into a single call, so the outer loop stays a clean filter.

Complexity

Where it breaks

Solution 2, walk friends-of-friends

Complexity

Where it lands

Benchmarks

Takeaways

Purchase Premium to Keep Reading

Unlock this article and so much more with Hello Interview Premium
Buy Premium

Guided Practice

Practice real problems with AI-powered feedback and hints.Start Guided Practice
Reading Progress

On This Page

Solution 1, scan every user

Complexity

Where it breaks

Solution 2, walk friends-of-friends

Complexity

Where it lands

Benchmarks

Takeaways

Questions
Meta SWE Interview QuestionsAmazon SWE Interview QuestionsGoogle SWE Interview QuestionsOpenAI SWE Interview QuestionsEngineering Manager (EM) Interview Questions
Learn
Learn System DesignLearn DSALearn BehavioralLearn ML System DesignLearn Low Level DesignGuided Practice
Links
FAQPricingGift PremiumHello Interview Premium
Legal
Terms and ConditionsPrivacy PolicySecurity
Contact
About UsProduct Support

7511 Greenwood Ave North Unit #4238 Seattle WA 98103


© 2026 Optick Labs Inc. All rights reserved.