Recommend friends of friends with mutual-friend counts, excluding direct friends and blocked users

Quick Overview

Write a SQL query that recommends friends of friends to each user from a directional friends table, leaving out the user, their direct friends and anyone they blocked, and counts mutual friends per recommendation. It tests self-joins, anti-joins with NOT EXISTS, distinct counting and multi-key ordering.

Recommend friends of friends with mutual-friend counts, excluding direct friends and blocked users

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Recommend friends for each user A: anyone who is a friend of one of A's friends, excluding A, A's direct friends, and users blocked by A. Return `user_id`, `recommended_friend`, and `mutual_friends` (the number of A's friends through whom the recommended user is reached). Order by `user_id` ascending, `mutual_friends` descending, then `recommended_friend` ascending. ```hint Paths in the graph Read each row of `friends` as an arrow from `user_id` to `friend_id`. A recommendation is where a path of two arrows starting at A ends. ``` ### Input Table(s) `friends` (no declared primary key) | Column | Type | Meaning | |---|---|---| | user_id | INT | The user, NOT NULL | | friend_id | INT | Someone `user_id` lists as a friend, NOT NULL | `page_likes` (no declared primary key) | Column | Type | Meaning | |---|---|---| | user_id | INT | The user, NOT NULL | | page_id | INT | A page the user liked, NOT NULL | `blocked_users` (no declared primary key) | Column | Type | Meaning | |---|---|---| | user_id | INT | The user who blocked someone, NOT NULL | | blocked_user_id | INT | The user they blocked, NOT NULL | ### Output Contract One row per (user, recommended user) pair, with these columns in this order: | Column | Type | Meaning | |---|---|---| | user_id | INT | User A | | recommended_friend | INT | The recommended user | | mutual_friends | integer | Number of distinct friends of A who list `recommended_friend` as a friend | Sort by `user_id` ascending, then `mutual_friends` descending, then `recommended_friend` ascending. A pair appears at most once, so this order is unique. No rounding is involved. ### Example `friends` | user_id | friend_id | |---|---| | 1 | 2 | | 1 | 3 | | 1 | 6 | | 2 | 1 | | 2 | 4 | | 2 | 5 | | 3 | 1 | | 3 | 4 | | 3 | 6 | | 4 | 6 | `page_likes` | user_id | page_id | |---|---| | 1 | 100 | | 4 | 100 | | 5 | 200 | `blocked_users` | user_id | blocked_user_id | |---|---| | 1 | 5 | | 6 | 2 | Expected result: | user_id | recommended_friend | mutual_friends | |---|---|---| | 1 | 4 | 2 | | 2 | 6 | 2 | | 2 | 3 | 1 | | 3 | 2 | 1 | User 1's friends are 2, 3 and 6. Both 2 and 3 list user 4, so 4 is recommended with 2 mutual friends. User 5 is reached through 2 but is blocked by user 1, user 6 is already user 1's direct friend, and paths that come back to user 1 are dropped. User 2 reaches 6 through both 1 and 4 and reaches 3 through 1. User 6 blocking user 2 does not matter, because only blocks made by user 2 filter user 2's recommendations. User 3 reaches 2 through 1, while 6 is already a direct friend. Users 4, 5 and 6 get no recommendations. ### Constraints and Clarifications - Each table has at most 10,000 rows, and no column is NULL. - `friends` is directional: (A, B) does not imply (B, A). A's friends, and A's direct friends for the exclusion, are the rows with `user_id = A`. - `blocked_users` is directional: A blocking B does not mean B blocks A. Only rows with `user_id = A` remove candidates for A. - A user cannot be friends with themselves, so there are no (A, A) rows. A can still be reached through a friend who lists A, and A is never recommended to A. - The block check applies to the recommended user only. A friend of A who connects A to the candidate is counted even if A has blocked that friend. - `mutual_friends` counts distinct friends of A. The tables have no declared key, so if a row appears twice it still counts once. - Users with no recommendations do not appear in the output. - `page_likes` is part of the schema but is not needed for this task.

Overview: Write a SQL query that recommends friends of friends to each user from a directional friends table, leaving out the user, their direct friends and anyone they blocked, and counts mutual friends per recommendation. It tests self-joins, anti-joins with NOT EXISTS, distinct counting and multi-key ordering.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Recommend friends for each user A: anyone who is a friend of one of A's friends, excluding A, A's direct friends, and users blocked by A. Return user_id, recommended_friend, and mutual_friends (the number of A's friends through whom the recommended user is reached). Order by user_id ascending, mutual_friends descending, then recommended_friend ascending.

Input Table(s)

friends (no declared primary key)

ColumnTypeMeaning
user_idINTThe user, NOT NULL
friend_idINTSomeone user_id lists as a friend, NOT NULL

page_likes (no declared primary key)

ColumnTypeMeaning
user_idINTThe user, NOT NULL
page_idINTA page the user liked, NOT NULL

blocked_users (no declared primary key)

ColumnTypeMeaning
user_idINTThe user who blocked someone, NOT NULL
blocked_user_idINTThe user they blocked, NOT NULL

Output Contract

One row per (user, recommended user) pair, with these columns in this order:

ColumnTypeMeaning
user_idINTUser A
recommended_friendINTThe recommended user
mutual_friendsintegerNumber of distinct friends of A who list recommended_friend as a friend

Sort by user_id ascending, then mutual_friends descending, then recommended_friend ascending. A pair appears at most once, so this order is unique. No rounding is involved.

Example

friends

user_idfriend_id
12
13
16
21
24
25
31
34
36
46

page_likes

user_idpage_id
1100
4100
5200

blocked_users

user_idblocked_user_id
15
62

Expected result:

user_idrecommended_friendmutual_friends
142
262
231
321

User 1's friends are 2, 3 and 6. Both 2 and 3 list user 4, so 4 is recommended with 2 mutual friends. User 5 is reached through 2 but is blocked by user 1, user 6 is already user 1's direct friend, and paths that come back to user 1 are dropped. User 2 reaches 6 through both 1 and 4 and reaches 3 through 1. User 6 blocking user 2 does not matter, because only blocks made by user 2 filter user 2's recommendations. User 3 reaches 2 through 1, while 6 is already a direct friend. Users 4, 5 and 6 get no recommendations.

Constraints and Clarifications

  • Each table has at most 10,000 rows, and no column is NULL.
  • friends is directional: (A, B) does not imply (B, A). A's friends, and A's direct friends for the exclusion, are the rows with user_id = A .
  • blocked_users is directional: A blocking B does not mean B blocks A. Only rows with user_id = A remove candidates for A.
  • A user cannot be friends with themselves, so there are no (A, A) rows. A can still be reached through a friend who lists A, and A is never recommended to A.
  • The block check applies to the recommended user only. A friend of A who connects A to the candidate is counted even if A has blocked that friend.
  • mutual_friends counts distinct friends of A. The tables have no declared key, so if a row appears twice it still counts once.
  • Users with no recommendations do not appear in the output.
  • page_likes is part of the schema but is not needed for this task.