cswork / libraryOpen practice workspace

Coding questions · Amazon

Find recommended friend pairs by shared listening

This question evaluates the ability to perform time-based deduplication, aggregation and pairwise matching using SQL (joins, grouping, and anti-joins), along with handling unordered user pairs and extracting calendar dates from timestamps, and falls under the Data Manipulation (SQL/Python) domain.

Problem (SQL)

You work on a music app and want to recommend new friend connections based on listening similarity.

Tables

Assume the following schemas:

listens

  • user_id (BIGINT)
  • song_id (BIGINT)
  • listened_at (TIMESTAMP)

friendships (undirected friendship; may be stored as one row per pair)

  • user_id_1 (BIGINT)
  • user_id_2 (BIGINT)
  • created_at (TIMESTAMP)

Definitions / assumptions

  • Two users have an overlapping song on a given day if both listened to the same song_id on that same calendar date.
  • Count distinct overlapping songs per pair per day (ignore repeat listens of the same song).
  • Treat pairs as unordered (e.g., output (min_user, max_user)).
  • A pair should be recommended only if they are not already friends (no matching pair in friendships, regardless of ordering).
  • Unless stated otherwise, treat listened_at as UTC when deriving the calendar date.

Task

Write a SQL query to return all recommended pairs of users (user_id_a, user_id_b) such that there exists at least one day where they listened to more than 3 of the same songs on that day.

Output

Return:

  • user_id_a
  • user_id_b
  • listen_date
  • overlap_song_count

Only include rows where overlap_song_count > 3 and the pair is not already in friendships.

Find recommended friend pairs by shared listening on the same day

You are given Spotify-like listening logs. Recommend new friend pairs based on shared music taste:

Return all pairs of users (user_a, user_b) who are NOT already friends, and who have listened to at least 4 of the same songs on the SAME calendar day (count distinct songs). A pair should appear once with user_a < user_b. If a pair qualifies on multiple days, return one row per qualifying day.

Output columns: user_a, user_b, listen_date, shared_song_count.

Table friendships

columntypeconstraints
user_id1INTNOT NULL
user_id2INTNOT NULL

Sample data:

user_id1user_id2
13
23

Table listens

columntypeconstraints
listen_idBIGINTPRIMARY KEY
user_idINTNOT NULL
song_idINTNOT NULL
listen_dateDATENOT NULL

Sample data:

listen_datelisten_idsong_iduser_id
2025-05-2011011
2025-05-2021021
2025-05-2031031
2025-05-2041041
2025-05-2051051
2025-05-2061012
2025-05-2071022
2025-05-2081032
2025-05-2091042
2025-05-20101062
2025-05-20111013
2025-05-20121023
2025-05-20131033
2025-05-20141043
2025-05-21151071
2025-05-21161081
2025-05-21171072
2025-05-21181092
2025-05-22192014
2025-05-22202024
2025-05-22212034
2025-05-22222044
2025-05-22232054
2025-05-22242015
2025-05-22252025
2025-05-22262035
2025-05-22272045
2025-05-22282045
2025-05-22292065

Expected output

Each row is a non-friend user pair that shares at least 4 distinct songs on a given day. Output includes the day and the number of shared distinct songs.

listen_dateshared_song_countuser_auser_b
2025-05-20412
2025-05-22445

Hints

  1. Self-join the listens table on (listen_date, song_id) to find shared songs between two users.
  2. Use user_id < other_user_id (or LEAST/GREATEST) so each pair appears once.
  3. Group by pair + date and HAVING COUNT(DISTINCT song_id) >= 4, then anti-join to friendships to exclude existing friends.