Leetcodify Friends Recommendations - Problem
๐ต Leetcodify Friends Recommendations
You're working at Leetcodify, the world's most popular music streaming platform! Your task is to build a smart friend recommendation system that connects users based on their music listening habits.
The system analyzes two key data sources:
- Listens Table: Tracks when users listen to songs (user_id, song_id, day)
- Friendship Table: Existing friendships between users (user1_id, user2_id)
Your algorithm should recommend user X to user Y if:
- ๐ซ Users X and Y are not already friends
- ๐ถ Users X and Y listened to the same 3+ different songs on the same day
Important Notes:
- Recommendations are bidirectional - if X should be recommended to Y, then Y should also be recommended to X
- No duplicate recommendations allowed
- The
Listenstable may contain duplicate rows - In
Friendshiptable, user1_id < user2_id always
Return a result table with columns: user_id, recommended_id
Input & Output
Basic Recommendation Example
$
Input:
Listens: [[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: []
โบ
Output:
[[1,2],[2,1]]
๐ก Note:
Users 1 and 2 listened to songs 10, 11, and 12 on the same day (2021-03-15). Since they have 3 common songs and are not friends, they should be recommended to each other.
Existing Friends Filtered Out
$
Input:
Listens: [[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'],[3,10,'2021-03-15'],[3,11,'2021-03-15'],[3,12,'2021-03-15']]
Friendship: [[1,2]]
โบ
Output:
[[1,3],[2,3],[3,1],[3,2]]
๐ก Note:
Users 1, 2, and 3 all have 3+ common songs. However, users 1 and 2 are already friends, so only 1โ3 and 2โ3 recommendations are made.
Insufficient Common Songs
$
Input:
Listens: [[1,10,'2021-03-15'],[1,11,'2021-03-15'],[2,10,'2021-03-15'],[2,12,'2021-03-15']]
Friendship: []
โบ
Output:
[]
๐ก Note:
Users 1 and 2 only have 1 common song (song 10) on 2021-03-15. Since we need 3+ common songs, no recommendations are made.
Constraints
- 1 โค Listens.length โค 105
- 1 โค Friendship.length โค 104
- 1 โค user_id, song_id โค 104
- Listens table may contain duplicates
- In Friendship table, user1_id < user2_id always
- All dates are in valid YYYY-MM-DD format
Visualization
Tap to expand
Understanding the Visualization
1
Data Collection
Gather all listening history and existing friendships
2
Deduplication
Remove duplicate listening records to get clean data
3
Daily Grouping
Group users by songs they listened to each day
4
Similarity Scoring
Count common songs between user pairs per day
5
Filtering
Keep pairs with 3+ common songs who aren't friends
6
Bidirectional Output
Generate mutual recommendations
Key Takeaway
๐ฏ Key Insight: Group users by their daily listening patterns first, then efficiently count song overlaps to identify strong musical compatibility signals for friendship recommendations.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code