Education + Jobs Hiring Website - 2025
0 like 0 dislike
18 views
Q2. Alice's Friend Recommendations

 

You are given a social network consisting of people and friendship relationships.

 

Each friendship is bidirectional.

 

Given a person "Alice", find the top 3 people Alice should be recommended as friends.

 

A person can be recommended only if:

 

- They are not Alice.

- They are not already a direct friend of Alice.

 

The recommendation score of a person is the number of Alice's direct friends who are also directly connected to that person.

 

Return the people with the highest recommendation score.

 

If multiple people have the same score, sort them alphabetically.

 

Example

 

Friends:

Alice Bob

Alice Carol

Alice David

 

Bob Gracie

Bob Eve

 

Carol Gracie

Carol Frank

 

David Gracie

David Harry

 

Alice's direct friends are:

 

Bob, Carol, David

 

Scores:

 

Gracie → 3

Eve → 1

Frank → 1

Harry → 1

 

Therefore:

 

Output:

Gracie Eve Frank

 

Constraints

 

- "1 <= N <= 10^5"

- "1 <= M <= 2 * 10^5"

- Names are unique strings.

- Each friendship appears at most once.

 

Return at most 3 recommended names.
ago in Online Assessments by Expert (139,750 points) | 18 views

Please log in or register to answer this question.