Number of Recent Calls

Implement RecentCounter, which counts requests inside a sliding time window. ping(t) records a request at time t milliseconds and returns how many requests happened in the inclusive range [t - 3000, t]. Every call uses a strictly larger t than the call before it.

30:00

Number of Recent Calls
easy
Free written solution
Topics
Companies

Implement RecentCounter, which counts requests inside a sliding time window. ping(t) records a request at time t milliseconds and returns how many requests happened in the inclusive range [t - 3000, t]. Every call uses a strictly larger t than the call before it.

Example 1:
Input: {"commands":["RecentCounter","ping","ping","ping","ping"],"values":[[],[1],[100],[3001],[3002]]}
Output: [null,1,2,3,3]
Constraints:
  • 1≤t≤1091 \leq t \leq 10^9

  • Each test case will call ping with strictly increasing values of t.

  • At most 10410^4 calls will be made to ping.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"commands":["RecentCounter","ping","ping","ping","ping"],"values":[[],[1],[100],[3001],[3002]]}
new
ping(1)
ping(100)
ping(3001)
ping(3002)

5 calls. Each ping counts the requests in the 3000 ms window ending at t.

time (ms)

no pings yet

queue (front on the left), length 0

empty

Returned values

[]
Variables
VariableValue
queue[]
output[]
DepthFunction Call
Stack empty
0/15