Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Top Songs Per User Rolling Window
00:00
5 left

Top Songs Per User Rolling Window

HardSQL · PostgreSQL

Problem

Given a schema of user play events, write a query to identify the top three most-played songs per user over a rolling seven-day window at Spotify.

Use each user's distinct play-event date as a window end date. A window includes plays from that date and the preceding six calendar days. Exclude incomplete events with NULL user, song, or timestamp values.

Output

  1. One row per ranked user, window end date, and song
  2. Columns: user_id, display_name, window_end_date, song_id, song_title, play_count, and song_rank
  3. Include only the top three songs per user and window, ordered by play count descending, song ID ascending for ties, then user ID and window date

Schema

users
ColumnTypeDescription
user_idPKINTUnique Spotify user identifier
display_nameVARCHAR(100)User display name
songs
ColumnTypeDescription
song_idPKINTUnique song identifier
song_titleVARCHAR(200)Song title
play_events
ColumnTypeDescription
play_event_idPKINTUnique play-event identifier
user_idINTUser who played the song
song_idINTSong that was played
played_atTIMESTAMPTimestamp when playback occurred
Tablesuserssongsplay_events
Interviewer

Your question is Top Songs Per User Rolling Window. Start with the requirements and the three tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.