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: user1_id < user2_id

This 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
๐ŸŽธ Finding Concert Buddies (Similar Friends)U1U2U3friendsfriendsConcert Attendance RecordsUser 1 - Jan 1stS1S2S3S4Songs: 101, 102, 103, 104User 2 - Jan 1stS1S2S3Songs: 101, 102, 103User 3 - Jan 1stS5Songs: 201๐ŸŽฏ Similar Friends Found!Users 1 & 2: 3 common songs on Jan 1stSongs 101, 102, 103 โœ“Users 2 & 3: Only 0 common songs โœ—3 matches!
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

n
2n
โšก Linearithmic
Space Complexity
O(F + L)

Space for friendship and listening data in memory

n
2n
โœ“ 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
Asked in
Spotify 45 Meta 38 Amazon 32 Google 28
43.5K Views
High Frequency
~25 min Avg. Time
1.8K 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