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:

  1. ๐Ÿšซ Users X and Y are not already friends
  2. ๐ŸŽถ 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 Listens table may contain duplicate rows
  • In Friendship table, 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.

Visualization

Tap to expand
๐ŸŽต Leetcodify Friend RecommendationsUser AMar 15: Songs 10,11,12Mar 16: Songs 13,14User BMar 15: Songs 10,11,12Mar 16: Songs 15,16Similarity AnalysisMarch 15thSong 10 โœ“Song 11 โœ“Song 12 โœ“โœ… Recommendation Made!3+ Common Songs + Not Friends = MatchCompare Listening HistoryResult: User A recommends User B AND User B recommends User A
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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(L log L + Uยฒ)

L log L for sorting/grouping listens, Uยฒ for user pair combinations

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

L for listening data storage, F for friendship lookups

n
2n
โœ“ Linear Space

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
Asked in
Spotify 45 Meta 35 Amazon 28 Google 22
21.1K Views
Medium-High 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