v9.1.1
Menu

Design Hit Counter

mediumFREEStreaming & QueuesNetflix DSA

Design a hit counter that counts hits in the past 5 minutes (300 seconds).

Operations: hit,timestamp records a hit, getHits,timestamp returns hits in past 300 seconds.

Output results of each getHits call.

Example

Input
hit,1;hit,2;hit,3;getHits,4;hit,300;getHits,300;getHits,301
Output
3
4
3
Reasoning
  • Initially, hits are recorded at timestamps 1, 2, and 3.
  • At timestamp 4, getHits returns 3 because all previous hits (at 1, 2, and 3) are within the past 300 seconds.
  • At timestamp 300, getHits returns 4 because the hit at timestamp 300 is added to the previous hits that are still within the 300-second window (1, 2, 3, and 300).
  • At timestamp 301, getHits returns 3 because the hits at timestamps 1 and 2 are no longer within the past 300 seconds (301−300=1301 - 300 = 1 second has passed, and 300−1=299300 - 1 = 299 seconds have passed since the first hit), leaving hits at 3 and 300 within the window.

Constraints

  • 1 <= timestamp <= 2 * 10^9
  • Timestamps are non-decreasing

Learn this in

More from Netflix DSA

solution.py

Test results

Sign in to run your code against the test cases.