BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Recommender systems22 of 22 in topic

767. People you may know: rank suggestions by mutual connections

MediumRecommender systems Pro

You are building the first version of "People you may know" for a professional network. The classic strong baseline: suggest people the user is not connected to yet, ranked by how many connections they share. The connection log is messy: the same connection is often logged twice (once from each side, or again after an app retry), and a few bad rows link a user to themselves.

Write suggest_friends(edges, user, k, blocked=None).
- edges: a list of (a, b) pairs of int user ids, meaning a and b are connected (undirected).
- Ignore self links (a == b). Count each connection once, however many times or in whichever order it appears. A duplicated row must not make someone look like they share more friends.
- Suggest up to k people who are not user, not already connected to user, not in blocked (a set, list or None), and share at least one connection with user.
- Rank by mutual connection count, highest first, then by smaller id.
- Return [] when k is 0 or nobody qualifies (for example, user has no connections).

Constraints: up to 200,000 edges, and a popular user can have thousands of connections.

Functiondef suggest_friends(edges, user, k, blocked=None)

Example 1

Input: edges = [(1, 2), (1, 3), (2, 4), (3, 4), (2, 5), (4, 6)]. suggest_friends(edges, 1, 2)

Output: [4, 5]: 4 shares connections 2 and 3, and 5 shares connection 2

Example 2

Input: suggest_friends(edges, 1, 2, blocked={4})

Output: [5]

Constraints

  • Up to 1,000,000 edges.
  • Ids are integers.
Hint 1

Use sets for neighbors.

Hint 2

Exclude the user, current friends and blocked ids.

Hint 3

Sort by (-count, id) or keep a heap of size k.

Common mistakes
  • Suggesting existing friends or the user.
  • Double counting because duplicate edges were kept in lists.
  • Sorting all candidates when only k are needed on a huge graph.
Understand the idea

Friends of friends is a two-hop walk on the graph. The number of distinct two-hop paths from you to a person equals the number of friends you share. It is a classic first-stage candidate generator for people suggestions before a learned ranker scores candidates with more features.

Topics
Recommender systems
Similar questions
  • Predict a rating from similar usersMedium
  • Take one SGD step of matrix factorisationMedium
  • Recommend unseen items with item-item cosine similarityMedium
  • Sample negative items a user has not interacted withMedium
All Recommender systems problems
Report
767 / 903
Python 3solution.pyPython idle
1
2
3
4
def suggest_friends(edges, user, k, blocked=None):
# return up to k suggested user ids
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS