Rolling 28-Day Active Users for Every Calendar Day
Last updated: November 29, 2026
Quick Overview
Given a table of user activity dates, compute for every day the number of distinct users active in the trailing 28 days, and explain how to make it scale when the activity table has billions of rows.
TikTok
November 29, 20260
0
0 solved
Given a table of user activity dates, compute for every day the number of distinct users active in the trailing 28 days, and explain how to make it scale when the activity table has billions of rows.
Monthly active users is a headline metric for TikTok, and the warehouse team needs a daily series of it per market. The interviewer checks that you know distinct counts cannot be summed across days, can write a correct range join, and can propose a cheaper formulation for a platform at TikTok's scale.
What the Interviewer Expects
- Explain why summing daily DAU does not give 28-day actives
- Write a correct calendar-to-activity range join with an exclusive lower bound
- Propose an activity-span or last-seen formulation that avoids a 28x fan-out
- Mention approximate distinct counting (HyperLogLog sketches) as an alternative
- Validate the two approaches against each other
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 produce the same metric per country when users travel?
- How would you backfill a year of history efficiently?
- When is an HLL estimate with about 1% error acceptable for this metric?
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
daily_active(user_id, activity_date); may contain duplicates.calendar(d)covers the reporting period.- 28-day actives on day
d= distinct users with activity in(d - 28, d], which is 28 ...
Approach
Baseline: join each calendar day to all activity in its window and count distinct users. Correct but each activity row is touched 28 times, which hurts at billions of rows.
Scalable: turn eac...