Count Product Page Hits Over the Trailing Five Minutes
Last updated: November 29, 2026
Quick Overview
Design a counter with `hit(timestamp)` and `get_hits(timestamp)` that returns how many hits occurred in the last 300 seconds, inclusive of the current second. Timestamps are in seconds and calls arrive in non-decreasing time order.
Amazon
November 29, 20260
0
0 solved
Design a counter with `hit(timestamp)` and `get_hits(timestamp)` that returns how many hits occurred in the last 300 seconds, inclusive of the current second. Timestamps are in seconds and calls arrive in non-decreasing time order.
A phone-screen design-a-structure warm-up in Amazon's style, framed around a lightweight per-host metric for detail page traffic. The interviewer checks a correct window boundary, constant memory when hits are bursty, and a sensible answer to concurrency and out-of-order follow-ups.
What the Interviewer Expects
- Picks a structure and states the window boundary precisely
- Offers a fixed 300-slot circular buffer for O(1) memory
- Handles many hits in the same second
- Explains the queue alternative and its memory cost
- Discusses concurrency or distribution when asked
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if timestamps can arrive slightly out of order?
- How would you make this thread-safe on a busy host?
- How would you aggregate counts across a fleet of 1000 hosts?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
get_hits(ts) counts hits with timestamp where . Calls are in non-decreasing time. Many hits can share a second, so memory should not grow with traffic....
High-Level Design
A queue of timestamps works but stores one entry per hit, which is unbounded during a traffic spike. Since resolution is one second and the window is 300 seconds, use two arrays of length 300 indexed ...