Skip to content
Decoder

Number of Recent Calls

Easy

Problem

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.

Example 1

c = RecentCounter()

Example 2

Input
c.ping(1)
Output
1
Why
range [-2999, 1]: just 1

Example 3

Input
c.ping(100)
Output
2
Why
1 and 100

Example 4

Input
c.ping(3001)
Output
3
Why
range [1, 3001]: 1, 100, 3001

Example 5

Input
c.ping(3002)
Output
3
Why
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.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.