Design Hit Counter
Design a hit counter that counts hits received in the past 5 minutes (300 seconds). hit(timestamp) records a hit at the given time in seconds. getHits(timestamp) returns how many hits happened in the previous 300 seconds, i.e. with time in the range (timestamp - 300, timestamp]. Calls arrive in non-decreasing timestamp order; several hits may share a timestamp.
Open official problem prompt ↗Report how many hits landed within the most recent 300-second window ending at the query time.
Like a turnstile counter that only cares about the last five minutes: as time moves forward, entries older than five minutes silently roll off the tally.
- Input
- hit(1); hit(2); hit(3); getHits(4); hit(300); getHits(300); getHits(301)
- Output
- 3; 4; 3
- Why
- At time 4 the hits at 1,2,3 count (=3); at 300 all four count (=4); at 301 the hit at time 1 falls outside (301-300=1, so 1 is excluded), leaving 3.
1 <= timestamp <= 2 * 10^9All calls are made in non-decreasing timestamp orderAt most 300 hits per second in the follow-up variantAt most 3 * 10^4 calls to hit and getHits