Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Design Hit Counter
Problem
Design a counter that records hits and reports how many hits occurred in the trailing 300-second window. Implement HitCounter with:
hit(timestamp): record one hit at the given time (seconds).getHits(timestamp): return the number of hits with time in(timestamp - 300, timestamp], i.e. the last 300 seconds inclusive oftimestamp.
Timestamps are non-decreasing across all calls, and several hits may share the same timestamp.
- Timestamps are positive and can grow into the billions of seconds.
- At most a few hundred calls to
hitandgetHitscombined in the base version; the follow-ups relax this.
Examples
hit(50),hit(51),hit(52),getHits(53)→3;getHits(349)→3because 50, 51, 52 are all within(49, 349];getHits(350)→2because the hit at time 50 has aged out.hit(9),hit(9),hit(9),getHits(9)→3; multiple hits at one timestamp all count.getHits(10)on a fresh counter →0.
Hints
- A hit at time
tis relevant only untilt + 300. Once it is older than that, it will never be relevant again. What does that suggest about the order in which hits can be discarded? - Timestamps are non-decreasing, so the oldest hit is always at the front. Which structure supports pop-from-front and push-to-back in O(1)?
- A queue of timestamps: on
getHits, pop from the front while the front is<= timestamp - 300, then return the queue length. To bound memory under heavy traffic, store(timestamp, count)pairs or use a 300-slot circular buffer.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.