Interview Query

John's New Best Friend

Start Timer

0:00:00

Upvote
11
Downvote
Save question
Mark as completed
View comments (20)
Next question

Using the provided database tables, determine a potential friend for John based on a point-based system. The recommendation system is compliant of the following weights:

  • Mutual Friends: +3 points for each mutual friend.
  • Page Likes: +2 points for every shared page like.
  • Blocked Users: Disqualified if John has blocked them before.
  • Already Friends: Disqualified if they are already John’s friends.

You have been given the following tables in the database:

users table:

Column Type
user_id INTEGER
name VARCHAR

This table holds information about various users, with user_id being a unique identifier for each user and name being the user’s name.

friends table:

Column Type
user_id INTEGER
friend_id INTEGER

This table lists all the friendships, with user_id and friend_id representing the two users who are friends.

likes table:

Column Type
user_id INTEGER
page_id INTEGER

This table records which pages each user has liked, with user_id and page_id representing the user and the page respectively.

blocks table:

Column Type
user_id INTEGER
blocked_id INTEGER

This table records which users each user has blocked, with user_id and blocked_id representing the user and the blocked user respectively.

Output:

Provide the result in the following format:

Column Type
potential_friend_name VARCHAR
friendship_points INTEGER

This table should list the top user who have the highest “friendship points” according to John’s criteria, along with their total “friendship points”.

Note: John’s user_id is 3

Example:

Input:

users table:

user_id name
1 Alice
2 Bob
3 John
4 Dave

friends table:

user_id friend_id
1 2
2 1
1 4
4 1

likes table:

user_id page_id
3 10
1 10
2 11
4 10

blocks table:

user_id blocked_id
3 4

Points Calculation (without already being John’s friend or blocked):

  1. Alice (user 1):

    • Mutual Friends with John: None
    • Mutual Likes with John: 1 (page 10) = 2 points
    • Total points for Alice = 2 points
  2. Bob (user 2):

    • Mutual Friends with John: None
    • Mutual Likes with John: None
    • Total points for Bob = 0 points
  3. Dave (user 4):

    • He is blocked by John, so he is disqualified and receives 0 points.

Based on the recalculated points, the output table would look like:

Output:

potential_friend_name friendship_points
Alice 2

From the output, Alice, with 2 points, is the most suitable potential friend for John based on the provided criteria.

.
.
.
.
.


Comments

Loading comments