def getRecommendedFriends(n: int, friendships: list[list[int]]) -> list[int]:
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:
x and y are different users who are not already friends, andx and y have the maximum number of common friends (users who are friends with both), andFor 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).
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.
-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.Two online assessments from 2025: a new-graduate OA in January, where this was the second problem after palindromic substrings and was described as a known forum problem, "a little hard"; and a Citadel Securities campus OA in April, paired with a tree problem. The January candidate passed with friend sets, per-user common-friend counts, and a fallback to the smallest-index non-friend when no one shares a friend. The April report compares the problem to LeetCode 1917.
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:
x and y are different users who are not already friends, andx and y have the maximum number of common friends (users who are friends with both), andFor 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).
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.
-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.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
Send them this page. It is free to read, no account needed.
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.