Number of Recent Calls

Easy

Topics
QueueDesignData Stream

Implement RecentCounter that counts recent requests within a time window. Implement ping(t) which records a request at time t (in milliseconds) and returns the number of requests that happened in the inclusive range [t - 3000, t]. Every call to ping uses a strictly larger t than the previous call.

Example 1

returnsRecentCounter()ping(1)1ping(100)2ping(3001)3ping(3002)3
Calls arrive with strictly increasing timestamps, which is the fact that makes a queue sufficient: anything that falls out of the 3000 ms window will never come back, so it can be discarded from the front for good.
Calls run top to bottom; the arrow shows what each one returns.
Input:  operations = ["RecentCounter","ping","ping","ping","ping"], values = [[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]

Constraints

  • 1 <= t <= 10^9
  • Each call to ping uses a strictly increasing t.
  • At most 10^4 calls to ping.
Editorial — intuition, approach, complexity and pitfalls

Intuition

Because timestamps only increase, requests older than t - 3000 can never count again. A queue lets you drop those stale timestamps from the front and report the size of what remains.

Approach

  1. Keep a queue of timestamps.
  2. On ping(t), enqueue t, then dequeue from the front while the front is < t - 3000.
  3. Return the queue size — the number of requests in [t - 3000, t].

Dry Run

RecentCounter()
Timestamps only ever increase, so anything that falls out of the window is gone
for good and a queue is enough -- nothing ever needs re-admitting.
  ping 1      cutoff 1 - 3000 = -2999   queue [1]                 -> 1
  ping 100    cutoff -2900              queue [1, 100]            -> 2
  ping 3001   cutoff 1                  queue [1, 100, 3001]      -> 3
              the 1 is exactly at the cutoff, and the window INCLUDES it, so it
              stays
  ping 3002   cutoff 2                  queue [100, 3001, 3002]   -> 3
              now the 1 is strictly below 2 and is dropped
answer null|1|2|3|3
The two middle rows are the whole lesson. At t=3001 the cutoff is exactly 1 and
the oldest call is exactly 1; the window is [t - 3000, t] inclusive, so it counts,
and the strict < in the while condition is what keeps it. Change that to <= and
the run returns 1|2|2|3 instead of 1|2|3|3 -- off by one, on one call only, which
is the kind of thing that survives a casual read.
Each timestamp is appended once and popped at most once, so the amortized cost per
ping is O(1) even though a single ping can pop many entries.

Complexity

  • Time — O(1) amortized: each timestamp is enqueued and dequeued at most once.
  • Space — O(w): the queue holds only timestamps inside the 3000 ms window.

Common Pitfalls

  • The window is inclusive of t - 3000, so drop only strictly smaller timestamps.

Full Solution — all languages

class RecentCounter:
    def __init__(self):
        from collections import deque
        self.calls = deque()

    def ping(self, t):
        self.calls.append(t)
        while self.calls[0] < t - 3000:
            self.calls.popleft()
        return len(self.calls)
Asked at Databricks Roblox
SensAI
Beta
Tap the mic to stop voice mode