AO
Back

Friend Recommendation by Most Common Friends

OAAsync OALast reported April 2025Low Frequency

Problem Overview

There are n users indexed 0 to n - 1, and m friendships given as a 2D array friendships, where each entry [a, b] is a friendship between users a and b.

User y is recommended to user x if:

  1. x and y are different users who are not already friends, and
  2. x and y have the maximum number of common friends (users who are friends with both), and
  3. if several users satisfy 1 and 2, the one with the smallest index is recommended.

For each of the n users, return the index of the user to recommend. If there is no recommendation available, report -1.

Complete getRecommendedFriends(n, friendships).

Example (as posted)

n = 5
friendships = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4]]
answer: [3, 2, 1, 0, 1]

The post cuts the answer off after [3, 2, 1, 0]; the last entry follows from the rules. User 4 is friends only with 3, who is also friends with 1 and 2, so 1 (the smaller index) is recommended.

Not settled by the prompt

  • What counts as "no recommendation available"? If a user shares no common friend with anyone they are not already friends with, is the answer -1, or the smallest-index non-friend (zero common friends is still the maximum)? One candidate who passed the assessment returned the smallest-index non-friend; state your assumption, and check it against the platform's sample cases if they show one.

Notes from the reports

  • An online-assessment problem, reported twice in 2025. One report compares it to LeetCode 1917.
  • No input sizes were reported.

What Reports Emphasize

What passers do: One candidate kept friends as a list of sets, counted common friends per user in a list of maps, took the highest count with the smallest index on a tie, and fell back to the smallest-index non-friend when nobody shared a friend; it passed

Practice

Write your own against 7 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Citadel with friends?

Send them this page. It is free to read, no account needed.

More Citadel Questions

Free preview

Every question in the Citadel catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 62 Citadel questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Citadel · OA · Reported 2× across candidate reports
Is this helpful?