AlgoMaster Logo

Friend Recommendations: Solution Walkthrough

Medium Priority15 min readUpdated November 11, 2026

Friend Recommendations is the library behind a small social app. It tracks friendships, blocked users, and deactivated accounts. Each user is an integer id, and the social graph is loaded from a text file of changes applied in order.

The round has four phases:

  • Phase 0: Understand the code. Read the existing implementation and fill in three blanks.
  • Phase 1: Fix one-sided friendships. A bug report says a friendship shows on one user's profile but not the other's.
  • Phase 2: Suggest friends. Add recommend(graph, user, k=5), which suggests people by mutual friend count.
  • Phase 3: Make suggestions faster. Put recommendations behind a cache that stays correct while the graph changes, at 10,000 users and up to 1,000,000 requests (300,000 in Python).

The code is small, and the Phase 2 algorithm is the familiar friends-of-friends count. What trips people up is the product rules. Blocks apply in both directions, deactivated accounts vanish, and a mutual friend only counts if both people can see them.

Phase 3 then turns on one question you have to answer exactly: when a single edge changes, whose recommendations can change with it?

Premium Content

Subscribe to unlock full access to this content and more premium articles.