Sessionize Apple TV+ Playback Heartbeats With a 30-Minute Gap
Last updated: November 29, 2026
Quick Overview
Given a table of playback heartbeat events per user, group events into viewing sessions where a new session starts after more than 30 minutes of inactivity, and report each user's number of sessions and average session length in seconds.
Apple
November 29, 20260
0
0 solved
Given a table of playback heartbeat events per user, group events into viewing sessions where a new session starts after more than 30 minutes of inactivity, and report each user's number of sessions and average session length in seconds.
Apple TV+ analytics teams turn raw player heartbeats into sessions before measuring engagement, binge behavior or feature impact. The interviewer checks the LAG plus running SUM pattern, careful boundary handling, and awareness of messy event data.
What the Interviewer Expects
- Uses LAG to compute the gap from the previous event per user
- Flags session starts and builds session IDs with a running SUM
- Handles the first event and the exact 30-minute boundary explicitly
- Aggregates to session level before averaging per user
- Discusses duplicates, clock skew and multi-device viewing
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you split sessions when a user switches devices mid-episode?
- How do you count sessions that cross midnight in daily metrics?
- How would you add the last heartbeat interval to session duration?
- How would you write this in pandas?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Assume playback_events(user_id, event_ts) where event_ts is epoch seconds (in Postgres, convert timestamps with EXTRACT(EPOCH FROM ts)). Heartbeats arrive roughly every 60 seconds while playing....
Approach
- LAG gets the previous event time per user.
- Flag a new session when there is no previous event or the gap exceeds 1800 s.
- A running SUM of the flag gives a session number per user.
- Aggregat...