Skip to content

Number of Recent Calls

EasyStack
Decode it first. Clues, constraints, pattern and plan before any code.

Build a class RecentCounter that counts recent requests.

  • ping(t) records a new request at time t (in milliseconds) and returns how many requests happened in the last 3000 milliseconds, meaning in the inclusive range [t - 3000, t], counting this one.

Every call uses a strictly larger t than the call before it.

c = RecentCounter()
c.ping(1)     →  1    (range [-2999, 1]: just 1)
c.ping(100)   →  2    (1 and 100)
c.ping(3001)  →  3    (range [1, 3001]: 1, 100, 3001)
c.ping(3002)  →  3    (range [2, 3002]: 100, 3001, 3002)

Constraints

  • 1 <= t <= 10^9
  • Each t is strictly larger than the previous one.
  • At most 10^4 calls are made to ping.