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.

Google
Data Manipulation (SQL/Python)
Data Scientist
Google
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Medium

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
Rolling windows
Distinct counts
Date spine
Self joins
Approximate counting
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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
  1. Reduce to distinct (user_id, d) pairs, which shrinks data massively.
  2. Build a date spine from min to max date.
  3. Join each spine date to user-days within [as_of_date - 6, as_of_date] and cou...

Submit Your Answer
Markdown supported