
Back to back repeats
mediumBack to back repeats
Spotify SQL Interview Question
A track played twice in a row is the strongest signal a listener can give Spotify without pressing a button, and it feeds the recommender more than a like does. The same track played twice in a day with other music in between is far weaker, so the two have to be told apart.
For each listener who did it at least once, return the listener_id and the number of plays that repeated the track played immediately before as repeat_plays.
Asked of
- Data Analyst
- Business Analyst
- BI Analyst
- Product Analyst
- Analytics Engineer
- Data Engineer
- Data Scientist
spotify_playsTable30 rows
| Column Name | Type |
|---|---|
| play_id | BIGINT |
| listener_id | VARCHAR |
| track_id | VARCHAR |
| played_at | TIMESTAMP |
| ms_played | BIGINT |
spotify_playsExample Input
| play_id | listener_id | track_id | played_at | ms_played |
|---|---|---|---|---|
| 5001 | L01 | T100 | 2024-03-04 08:02:00 | 210000 |
| 5002 | L01 | T104 | 2024-03-04 08:06:00 | 185000 |
| 5003 | L01 | T100 | 2024-03-04 08:10:00 | 198000 |
| 5004 | L01 | T112 | 2024-03-04 09:15:00 | 240000 |
| 5005 | L01 | T104 | 2024-03-04 09:21:00 | 172000 |
| 5006 | L01 | T100 | 2024-03-04 18:40:00 | 205000 |
| 5007 | L01 | T118 | 2024-03-04 18:44:00 | 160000 |
| 5008 | L01 | T112 | 2024-03-04 18:49:00 | 233000 |
| 5026 | L04 | T100 | 2024-03-04 11:00:00 | 204000 |
| 5027 | L04 | T100 | 2024-03-04 11:05:00 | 201000 |
| 5028 | L04 | T104 | 2024-03-04 15:45:00 | 178000 |
| 5029 | L04 | T118 | 2024-03-04 15:52:00 | 162000 |
| 5030 | L04 | T121 | 2024-03-04 15:58:00 | 249000 |
Example Output
| listener_id | repeat_plays |
|---|---|
| L04 | 1 |
Explanation
L04 played T100 and then played it again straight away, which is one immediate repeat. L01 played T100 three times during the day but always with another track in between, so none of those count and L01 is not in the output.
The example above is a small slice of the data. Your query runs against the full tables.
Company
Spotify
Difficulty
medium
Topic
window functions
Language
SQL
Your query
Starting the SQL engine...