Design Hit Counter - Problem

Design a hit counter which counts the number of hits received in the past 5 minutes (i.e., the past 300 seconds).

Your system should accept a timestamp parameter (in seconds granularity), and you may assume that calls are being made to the system in chronological order (i.e., timestamp is monotonically increasing). Several hits may arrive roughly at the same time.

Implement the HitCounter class:

  • HitCounter() Initializes the object of the hit counter system.
  • void hit(int timestamp) Records a hit that happened at timestamp (in seconds). Several hits may happen at the same timestamp.
  • int getHits(int timestamp) Returns the number of hits in the past 5 minutes from timestamp (i.e., the past 300 seconds).

Input & Output

Example 1 — Basic Hit Counter Usage
$ Input: operations = ["HitCounter", "hit", "hit", "hit", "getHits", "hit", "getHits", "getHits"], values = [[], [1], [2], [3], [4], [300], [300], [301]]
Output: [null, null, null, null, 3, null, 4, 3]
💡 Note: Initialize counter, record hits at times 1,2,3. At time 4, all 3 hits are within 300 seconds. Hit at time 300. At time 300, all 4 hits are recent. At time 301, hit at time 1 is now 300+ seconds old, so only 3 hits remain.
Example 2 — Multiple Hits Same Time
$ Input: operations = ["HitCounter", "hit", "hit", "getHits"], values = [[], [1], [1], [2]]
Output: [null, null, null, 2]
💡 Note: Two hits at the same timestamp (time 1). getHits(2) counts both hits since they occurred within the past 300 seconds.
Example 3 — Edge Case Window Boundary
$ Input: operations = ["HitCounter", "hit", "getHits", "getHits"], values = [[], [1], [300], [301]]
Output: [null, null, 1, 0]
💡 Note: Hit at time 1. At time 300, the hit is exactly 299 seconds ago (within window). At time 301, the hit is exactly 300 seconds ago (outside window).

Constraints

  • 1 ≤ timestamp ≤ 2 * 109
  • All the calls are being made to the system in chronological order
  • At most 300 calls will be made to hit and getHits

Visualization

Tap to expand
Design Hit Counter - Bucket Array Approach INPUT Operations: HitCounter() hit(1) hit(2) hit(3) getHits(4) hit(300) getHits(300) getHits(301) 300 Buckets (mod 300): [0] 0 [1] 1 [2] 1 [3] 1 ... [299] 0 Timestamps Array: [0] 0 [1] 1 [2] 2 [3] 3 ... [299] 0 Window: 300 seconds (5 min) Index = timestamp % 300 Tracks hits + timestamps Stale data detected by time ALGORITHM STEPS 1 Initialize Arrays hits[300] = counts per bucket times[300] = last timestamp 2 hit(timestamp) idx = timestamp % 300 if times[idx] != timestamp: reset hits[idx] = 1 else: hits[idx]++ 3 getHits(timestamp) Sum all valid buckets: for i in 0..299: if timestamp - times[i] < 300 total += hits[i] 4 Stale Data Check Old entries auto-expire when timestamp diff >= 300 Time: hit O(1), getHits O(300) Space: O(300) = O(1) FINAL RESULT Execution Trace: Operation Result HitCounter() null hit(1) null hit(2) null hit(3) null getHits(4) 3 hit(300) null getHits(300) 4 getHits(301) 3 getHits(301): hit at t=1 expired (301-1 = 300 >= 300) Only hits at 2,3,300 counted Output: [null,null,null,null,3,null,4,3] Key Insight: Use a fixed-size bucket array of 300 (one per second in the window). Each bucket stores the hit count and the timestamp of the last hit. When querying, sum counts only from buckets where (currentTime - storedTime) < 300, effectively detecting and ignoring stale entries from previous cycles. TutorialsPoint - Design Hit Counter | Bucket Array (Fixed Window) Approach
Asked in
Google 25 Amazon 20 Facebook 18 Microsoft 15
35.0K Views
High Frequency
~25 min Avg. Time
980 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen