Leetcodify Similar Friends - Problem
Leetcodify Similar Friends
Imagine you're working for Leetcodify, a music streaming platform, and you want to identify users who have similar music tastes and are already friends. This can help improve music recommendations and create better social features!
You have two tables:
๐ต Listens Table: Records every time a user listens to a song on a specific day
๐ฅ Friendship Table: Contains pairs of users who are friends
Your Goal: Find pairs of friends who are "similar friends" - meaning they listened to 3 or more of the same songs on the same day.
Important Rules:
โข Only consider users who are already friends
โข They must have listened to the same songs on the same exact day
โข Need at least 3 different songs in common on that day
โข Return friend pairs in the same format as input:
This problem tests your ability to work with multiple tables, aggregate data, and apply complex filtering conditions - a common scenario in real-world applications!
Imagine you're working for Leetcodify, a music streaming platform, and you want to identify users who have similar music tastes and are already friends. This can help improve music recommendations and create better social features!
You have two tables:
๐ต Listens Table: Records every time a user listens to a song on a specific day
๐ฅ Friendship Table: Contains pairs of users who are friends
Your Goal: Find pairs of friends who are "similar friends" - meaning they listened to 3 or more of the same songs on the same day.
Important Rules:
โข Only consider users who are already friends
โข They must have listened to the same songs on the same exact day
โข Need at least 3 different songs in common on that day
โข Return friend pairs in the same format as input:
user1_id < user2_idThis problem tests your ability to work with multiple tables, aggregate data, and apply complex filtering conditions - a common scenario in real-world applications!
Input & Output
example_1.sql โ Basic Similar Friends
$
Input:
Listens:
| user_id | song_id | day |
|---------|---------|----------|
| 1 | 101 | 2023-01-01 |
| 1 | 102 | 2023-01-01 |
| 1 | 103 | 2023-01-01 |
| 2 | 101 | 2023-01-01 |
| 2 | 102 | 2023-01-01 |
| 2 | 103 | 2023-01-01 |
| 3 | 201 | 2023-01-02 |
Friendship:
| user1_id | user2_id |
|----------|----------|
| 1 | 2 |
| 1 | 3 |
โบ
Output:
| user1_id | user2_id |
|----------|----------|
| 1 | 2 |
๐ก Note:
Users 1 and 2 are friends and listened to 3 common songs (101, 102, 103) on the same day (2023-01-01). Users 1 and 3 are friends but have no common listening activity.
example_2.sql โ Multiple Days
$
Input:
Listens:
| user_id | song_id | day |
|---------|---------|----------|
| 1 | 101 | 2023-01-01 |
| 1 | 102 | 2023-01-01 |
| 1 | 103 | 2023-01-02 |
| 2 | 101 | 2023-01-01 |
| 2 | 102 | 2023-01-01 |
| 2 | 103 | 2023-01-02 |
| 2 | 104 | 2023-01-02 |
| 2 | 105 | 2023-01-02 |
Friendship:
| user1_id | user2_id |
|----------|----------|
| 1 | 2 |
โบ
Output:
| user1_id | user2_id |
|----------|----------|
| | |
๐ก Note:
Users 1 and 2 are friends, but they only have 2 common songs on 2023-01-01 and 1 common song on 2023-01-02. Neither day has 3+ common songs, so they're not similar friends.
example_3.sql โ Edge Case: Exact Threshold
$
Input:
Listens:
| user_id | song_id | day |
|---------|---------|----------|
| 1 | 101 | 2023-01-01 |
| 1 | 102 | 2023-01-01 |
| 1 | 103 | 2023-01-01 |
| 1 | 101 | 2023-01-01 |
| 2 | 101 | 2023-01-01 |
| 2 | 102 | 2023-01-01 |
| 2 | 103 | 2023-01-01 |
Friendship:
| user1_id | user2_id |
|----------|----------|
| 1 | 2 |
โบ
Output:
| user1_id | user2_id |
|----------|----------|
| 1 | 2 |
๐ก Note:
Even though user 1 listened to song 101 twice on the same day, we count DISTINCT songs. Users 1 and 2 share exactly 3 distinct songs (101, 102, 103) on 2023-01-01, which meets the threshold.
Visualization
Tap to expand
Understanding the Visualization
1
Friendship Network
Start with your network of friends from the Friendship table
2
Concert Attendance
Map out who attended which concerts (songs) on which nights (days)
3
Find Overlaps
For each friend pair, find nights where they attended same concerts
4
Count & Filter
Keep friend pairs who attended 3+ same concerts on any single night
Key Takeaway
๐ฏ Key Insight: Use SQL JOINs to connect friendship data with listening data, then GROUP BY user pairs and days to efficiently count common songs. The HAVING clause filters for 3+ matches, making this much more efficient than checking each friendship individually.
Time & Space Complexity
Time Complexity
O(F + L log L)
F friendships + L listens with logarithmic JOIN optimization
โก Linearithmic
Space Complexity
O(F + L)
Space for friendship and listening data in memory
โ Linear Space
Constraints
- 1 โค Number of users โค 104
- 1 โค song_id โค 106
- 1 โค Number of friendships โค 104
- 1 โค Number of listening records โค 105
- user1_id < user2_id in Friendship table
- Dates are in valid YYYY-MM-DD format
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code