Friend Recommender

Asked at MetaOnsite

Reported by the problem's creator, not verified

A social network stores friendships as a list of pairs [a, b], meaning users a and b are friends. A pair may appear more than once and in either order, but counts only once. Implement recommend(friendships, user, k), which returns up to k user ids to suggest to user. A suggestion is never user and never someone who is already a friend of user.

Examples

  1. Example 1

    Input: friendships = [[1,2],[1,3],[2,4],[3,4],[2,5],[6,7]], user = 1, k = 2

    Output: [4,5]

    Explanation: User 1's friends are 2 and 3. User 4 is a friend of both (2 mutual friends) and user 5 is a friend of 2 only (1 mutual friend). Users 6 and 7 share no friend with 1, so they are not suggested. Both candidates fit in k = 2.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.