Design a Hit Counter
📈 What is a hit counter? It is a small component that counts events as they happen, such as page views, API calls or failed logins. At any moment you can ask it "how many events arrived in the last five minutes?" and get an exact answer. Dashboards, rate limiters and alerting rules are all built on top of something like it.
A hit counter is one of the shortest problems you will see in a low-level design round, and that is exactly why interviewers like it. The first answer that works takes five minutes. The interview is about what you do next: noticing that memory grows with traffic, choosing a structure whose size does not, and explaining the cost of every operation without being asked.
Requirements
"Design a hit counter that counts the number of hits received in the past 5 minutes."
That is usually the whole prompt. It hides a few decisions: what "the past 5 minutes" means at the edges, how time reaches the counter, and how busy the counter is. Ask about each one.
Clarifying Questions
You: How precise is time? Seconds, milliseconds?
Interviewer: Seconds are fine. Every hit happens at a whole second, and "5 minutes" means 300 seconds.
You: Is the window inclusive at both ends? If a hit happened at second 1, does it still count at second 301?
Interviewer: No. A query at time t counts hits from the 300 seconds ending at t: times t - 299 up to t. A hit exactly 300 seconds old has dropped out.
You: How does the counter know the time? Should it read the system clock?
Interviewer: Pass the timestamp into every call. You can assume calls arrive in order: a timestamp is never smaller than the one before it.
You: Can several hits share the same second?
Interviewer: Yes. In fact, assume traffic can be very heavy, thousands of hits in one second.
You: Is it one counter, or one per page or per user?
Interviewer: One counter for now. We may come back to that.
You: Do several threads call it at once?
Interviewer: Start single threaded. Be ready to talk about it.
The heavy-traffic answer is the one that shapes the design. It rules out any structure that stores one entry per hit, because the number of hits in five minutes has no upper bound. The window answer matters for the tests: "older than 300 seconds" and "300 seconds or older" give different counts on the boundary, so settle it out loud.
💡 Interview tip: Whenever a problem says "in the last N minutes", ask two things before you code: is the edge inclusive, and are timestamps in order? The first decides an off-by-one in every test. The second decides whether you can throw old data away safely.
Final Requirements
hit(timestamp)records one hit at that second.getHits(timestamp)returns how many hits happened in the 300 seconds ending attimestamp: every hit with a time greater thantimestamp - 300.- Timestamps are in seconds and never decrease from one call to the next. Many hits may share a second.
- Memory must stay bounded no matter how many hits arrive.
- Both operations should be fast, ideally independent of the number of hits.
Out of scope
- Separate counters per page or per user (we come back to this in Extensibility).
- Counting across many servers.
- Precision finer than one second.
- Saving counts to disk.