Compute Rolling Seven-Day Active Viewers From YouTube Watch Events
Last updated: November 29, 2026
Quick Overview
Given a table of YouTube watch events, compute for each date the number of distinct users who watched at least once in the 7 days ending that date.
0
0
0 solved
Given a table of YouTube watch events, compute for each date the number of distinct users who watched at least once in the 7 days ending that date.
Rolling actives are a standard dashboard metric at YouTube, and the distinct-count requirement trips up candidates who reach for window functions. Interviewers check correct window boundaries, deduplication, and handling of days with no data.
What the Interviewer Expects
- Recognize COUNT(DISTINCT) cannot be used as a sliding window in most engines
- Deduplicate to user-day first
- Join a date spine to the activity within the window
- Get inclusive window boundaries right (6 days back plus today)
- Discuss cost and approximate alternatives
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 compute 28-day actives cheaply every day at YouTube scale?
- How would you add a breakdown by country when users travel?
- How would you use HyperLogLog sketches here?
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
watch_events(user_id STRING, event_ts TIMESTAMP, video_id STRING), one row per watch, many per user per day. Dates in UTC. Output: as_of_date, active_7d. Include dates even if no one watched (not ...
Approach
- Reduce to distinct
(user_id, d)pairs, which shrinks data massively. - Build a date spine from min to max date.
- Join each spine date to user-days within
[as_of_date - 6, as_of_date]and cou...