Two counters receive the same hits. To keep it watchable the window here is WINDOW = 12 s; the problem asks for 300 s, and the code is exactly the same.
A page records a hit every time someone visits it. At any moment you must be able to say how many hits came in during the last 300 seconds.
What you get
Two kinds of calls: hit(timestamp) records one hit, and getHits(timestamp) asks for the count. Timestamps are in seconds and never go backwards. Several hits can share a second.
What you return
getHits(t) returns how many hits have a timestamp after t − 300 and at most t. hit returns nothing.
The rules
A hit that is exactly 300 seconds old no longer counts. A hit 299 seconds old still does.
Every hit in the same second counts, not just one per second.
hit(10), hit(10), hit(150): three hits recorded, two of them in the same second.
getHits(200): the window reaches back 300 seconds, past the first hit, so all three count. The answer is 3.
hit(305): a fourth hit.
getHits(309): the window covers timestamps after 9, up to 309. The hits at 10, 10, 150 and 305 are all inside: 4.
getHits(310): the window now covers timestamps after 10. The two hits at 10 are exactly 300 seconds old and drop out: 2.
The visualizer below shrinks the window to 12 seconds so you can watch hits expire. The code is the same.
Edge cases to check
The boundary: at t, a hit from t − 300 is out and a hit from t − 299 is in.
A burst of hits in one second: storing every hit costs memory for each one.
A long quiet period: old hits must still be cleared out, or they keep using memory.
The usual follow up: what if there are millions of hits per second?
The question behind the solutions
Keeping every timestamp and dropping old ones from the front is simple and exact, but its memory grows with traffic. Can you answer with a fixed amount of memory, however busy the page gets?
Both counters
The calls from above with the real 300 second window, sketched the way you would draw it in an interview: hit(10), hit(10), hit(150), hit(305), then getHits(309) and getHits(310). Then watch both run below, with the window shrunk to 12 seconds.
1. Queue of timestamps
Remembers: the timestamp of every hit in the window, oldest at the front.
hit(t): add t to the back.
getHits(t): drop from the front while the front is ≤ t − 300.
Return the size of the queue.
The catch: exact, but a million hits a second means a million entries a second, and old hits stay in memory until the next getHits clears them.
2. Circle of 300 buckets
Remembers: two arrays of 300: the second each slot was last used for, and its hit count.
hit(t): slot i = t % 300. If times[i] is not t, the slot is stale: set it to t and its count to 0. Add 1.
getHits(t): add up the counts of every slot with t − times[i] < 300.
The catch: fixed memory however busy the page is, but it only knows whole seconds, and every getHits reads all 300 slots.
Two ways to solve it, step by step
Traffic
Speed
timestamp0
Hits per secondshaded = the window getHits(now) counts: (now − 12, now]
inside the windowoutside the window but still storedwindowbucket for the current second
Queue of timestamps
hit O(1)getHits O(1) amortizedspace O(hits in window), grows with traffic
Circle of 12 buckets
24 ints, always
hit O(1)getHits O(WINDOW) = O(1)space O(WINDOW), fixed
Last getHits call
–queue
–buckets
Complexity
Algorithm
hit()
getHits()
Space
Precision
Best for
Queue of timestamps
O(1)
O(1) amortized
O(hits in window)
Exact timestamps
Low traffic
Circle of buckets
O(1)
O(WINDOW)
O(WINDOW)
1-second buckets
High traffic, the usual follow-up answer
Versus a rate limiter: a hit counter only records and reports; it never rejects anything. Add if (getHits(now) < limit) hit(now) as one locked step and the queue version becomes SlidingWindowLogRateLimiter.