Design a Per-Video View Counter Over a Five Minute Window

Last updated: November 29, 2026

Quick Overview

Implement `hit(video_id, ts)` and `count(video_id, ts)`, where count returns how many views a video received in the last 300 seconds, that is in the window `(ts - 300, ts]`. Calls arrive with non-decreasing timestamps in seconds.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Implement `hit(video_id, ts)` and `count(video_id, ts)`, where count returns how many views a video received in the last 300 seconds, that is in the window `(ts - 300, ts]`. Calls arrive with non-decreasing timestamps in seconds.

Real-time view counts drive TikTok's trending detection and creator analytics, and this is a common phone-screen data structure question. The interviewer checks that you pick a structure that evicts old data cheaply, state the window boundary precisely, and can discuss bounded memory when traffic spikes.

What the Interviewer Expects
  • Clarifies whether the window is inclusive at each end
  • Uses a deque per video with eviction from the front
  • Amortized O(1) per operation and correct explanation of why
  • Handles unknown videos without creating junk state
  • Proposes fixed-size buckets when memory must be bounded
Key Topics to Cover
sliding window
queues
amortized analysis
data structure design
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • A viral video gets 100k views per second; how do you bound memory?
  • Timestamps can now arrive up to 10 seconds out of order; what changes?
  • How would you shard this across machines and get a global count?
  • How would you return the top 10 most viewed videos in the window?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

hit records one view at time ts. count returns views with timestamp in (ts - 300, ts]. Timestamps are non-decreasing across calls, so I can evict from the front permanently. Multiple hits can ...

High-Level Design

Keep a dictionary from video id to a deque of hit timestamps. hit appends to the right. count pops from the left while the oldest timestamp is at or before ts - 300, then returns the deque lengt...


Submit Your Answer
Markdown supported