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
xandyare not already friends - Users
xandylistened 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
| 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 |
| 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) |
Input & Output
| 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 |
| user1_id | user2_id |
|---|---|
| 1 | 3 |
| user_id | recommended_id |
|---|---|
| 1 | 2 |
| 2 | 1 |
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.
| 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 |
| user1_id | user2_id |
|---|---|
| 1 | 2 |
| user_id | recommended_id |
|---|
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 -
dayis a valid date -
In Friendship table,
user1_id < user2_id - Listens table may contain duplicate rows