Aggregate Per-User Watch Time From Logs Larger Than Memory
Last updated: November 29, 2026
Quick Overview
A machine with 2 GB of RAM must compute total watch milliseconds per user from a 200 GB log file of lines 'user_id,video_id,watch_ms'. Explain why the obvious dictionary approach fails and implement a bounded-memory version.
TikTok
November 29, 20260
0
0 solved
A machine with 2 GB of RAM must compute total watch milliseconds per user from a 200 GB log file of lines 'user_id,video_id,watch_ms'. Explain why the obvious dictionary approach fails and implement a bounded-memory version.
TikTok's data platform processes enormous watch logs, and engineers are expected to reason about memory before reaching for a cluster. This onsite fundamentals question checks streaming I/O, hash partitioning to disk, and estimating memory use per key.
What the Interviewer Expects
- Estimates memory: hundreds of millions of distinct users times roughly 100 bytes per Python dict entry exceeds RAM
- Streams the input line by line with a generator rather than reading it all
- Hash-partitions records into P spill files so each partition's distinct keys fit in memory
- Aggregates one partition at a time and writes results out
- Discusses choosing P, skewed keys, and when to switch to Spark or a sort-based approach
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you pick the number of partitions without knowing the distinct user count?
- What if one bot user accounts for 30 percent of lines?
- How would you compute the median watch time per user instead of the sum?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Input: huge CSV, one record per line, possibly some malformed lines.
- Output:
user_id,total_msfor every user, any order. - Memory budget: far below the size of the distinct key set....
High-Level Design
A dict of every user does not fit: with 500 million users, a CPython dict entry with a string key and int value is easily 100+ bytes, so 50+ GB. Two passes fix this:
- Partition pass: stream lin...