Leetcodify Friends Recommendations - Problem

You are working on a music streaming platform called Leetcodify. Your task is to create a friend recommendation system based on listening habits.

Tables:

  • Listens: Records when users listen to songs on specific days
  • Friendship: Tracks existing friendships between users

Recommendation Rules:

  • Users x and y are not already friends
  • Users x and y listened to the same 3 or more different songs on the same day
  • Recommendations are bidirectional (if x should be recommended to y, then y should also be recommended to x)
  • No duplicate recommendations

Return all friend recommendations with columns user_id and recommended_id.

Table Schema

Listens
Column Name Type Description
user_id int ID of the user who listened
song_id int ID of the song that was played
day date Date when the song was listened to
Primary Key: None (table may contain duplicates)
Note: Records user listening history with possible duplicates
Friendship
Column Name Type Description
user1_id PK int First user in friendship (always smaller ID)
user2_id PK int Second user in friendship (always larger ID)
Primary Key: (user1_id, user2_id)
Note: Stores friendships where user1_id < user2_id

Input & Output

Example 1 — Basic Friend Recommendations
Input Tables:
Listens
user_id song_id day
1 10 2024-03-15
1 11 2024-03-15
1 12 2024-03-15
2 10 2024-03-15
2 11 2024-03-15
2 12 2024-03-15
3 10 2024-03-15
3 11 2024-03-15
Friendship
user1_id user2_id
1 3
Output:
user_id recommended_id
1 2
2 1
💡 Note:

Users 1 and 2 both listened to songs 10, 11, and 12 on 2024-03-15 (3 shared songs), and they are not friends. Users 1 and 3 listened to songs 10 and 11 together (only 2 shared songs), which doesn't meet the 3+ requirement. Also, users 1 and 3 are already friends, so no recommendation is made.

Example 2 — Multiple Days and No Recommendations
Input Tables:
Listens
user_id song_id day
1 10 2024-03-15
1 11 2024-03-15
2 10 2024-03-16
2 11 2024-03-16
3 20 2024-03-15
4 21 2024-03-15
Friendship
user1_id user2_id
1 2
Output:
user_id recommended_id
💡 Note:

No recommendations are made because: Users 1 and 2 listened to same songs but on different days (must be same day). Users 3 and 4 listened on the same day but to completely different songs. No pair of users has 3+ shared songs on the same day while not being friends.

Constraints

  • 1 ≤ user_id ≤ 1000
  • 1 ≤ song_id ≤ 100000
  • day is a valid date
  • In Friendship table, user1_id < user2_id
  • Listens table may contain duplicate rows

Visualization

Tap to expand
Leetcodify Friends Recommendations INPUT Listens Table user_id song_id day 1 10 2021-03-15 1 11 2021-03-15 1 12 2021-03-15 2 10 2021-03-15 2 11 2021-03-15 2 12 2021-03-15 Friendship Table user1_id user2_id 1 3 User Network: U1 U2 U3 ALGORITHM STEPS 1 Self-Join Listens Match users with same song_id AND day 2 Filter Different Users WHERE L1.user_id < L2.user_id 3 Exclude Friends LEFT JOIN Friendship WHERE friend IS NULL 4 Count and Filter GROUP BY user pair, day HAVING COUNT >= 3 Matching Songs (Day: 03-15) U1: 10 U1: 11 U1: 12 U2: 10 U2: 11 U2: 12 OK OK OK FINAL RESULT Recommendations Table user_id friend_id 1 2 2 1 Bidirectional! U1 --> U2 AND U2 --> U1 New Connection: U1 U2 RECOMMEND 3+ Songs Match - OK Key Insight: Self-join the Listens table to find user pairs who listened to the same songs on the same day. Use LEFT JOIN with Friendship to exclude existing friends. UNION generates bidirectional pairs. HAVING COUNT(*) >= 3 ensures at least 3 common songs on the same day for recommendation. TutorialsPoint - Leetcodify Friends Recommendations | Optimal Solution
Asked in
Spotify 28 Amazon 22 Meta 18 Apple 15
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen