EasyDesignQueueData StreamUpdated Sep 2026

933. Number of Recent Calls

Asked at Databricks

Difficulty

Easy

Time

O(1) amortized per ping

Space

O(w), where w is the number of r

Pattern

Design

Companies

1

Problem

Design a class called RecentCounter that counts how many requests arrived in the last 3000 milliseconds.

The constructor RecentCounter() starts with no requests recorded. The method ping(t) records a new request at time t (in milliseconds) and returns how many requests fall inside the inclusive window [t - 3000, t]. That window includes the new request itself, plus every earlier request whose timestamp is at least t - 3000.

You are guaranteed that each ping call uses a strictly larger t than the previous call, so timestamps always arrive in increasing order. Once a request is too old for the current window, it can never become recent again.

Which requests count as recent

requests[2, 3002]110030013002At ping(3002) the window is [2, 3002]; time 1 is tooold, so the answer is 3.
A request counts only if its timestamp is at least t minus 3000; older ones are outside the window.

Constraints

  • ●1 <= t <= 10^9
  • ●Each test case calls ping with strictly increasing values of t
  • ●At most 10^4 calls will be made to ping

Examples

Example 1
Input["RecentCounter", "ping", "ping", "ping", "ping"] [[], [1], [100], [3001], [3002]]
Output[null, 1, 2, 3, 3]
Whyping(1) records 1; the window [-2999, 1] holds only that request, so we return 1. ping(100) adds 100; both 1 and 100 are inside [-2900, 100], so we return 2. ping(3001) adds 3001; the window is [1, 3001], and 1 is still inside it, so all three requests count and we return 3. ping(3002) adds 3002; no
Example 2
Input["RecentCounter", "ping", "ping"] [[], [5], [3000]]
Output[null, 1, 2]
Whyping(5) returns 1. For ping(3000) the window is [0, 3000], and the request at time 5 is still inside it, so both requests count and we return 2.
Example 3
Input["RecentCounter", "ping", "ping"] [[], [1], [3002]]
Output[null, 1, 1]
Whyping(1) returns 1. For ping(3002) the window is [2, 3002]; the request at time 1 is smaller than 2, so it is dropped and only the new request counts — we return 1.

Asked At

1 companies
CompanyDifficulty
DatabricksEasyView all Databricks questions →

How to Think About It

1

Store all ping timestamps in a list or queue

2

For each new ping, remove all timestamps older than t-3000

3

Count the remaining timestamps in the window [t-3000, t]

4

A deque or queue is ideal for efficient removal from the front

5

The amortized cost per ping is O(1) since each element is added and removed once

Optimal Approach

Maintain a queue of timestamps. On each ping(t), first remove all timestamps from the front that are less than t-3000 (outside the window). Then add t to the queue. The size of the queue is the number of recent calls. Since each timestamp is added once and removed once, the amortized time per ping is O(1).

Queue holds only the live window

queue11003001frontbackBefore ping(3002): front is 1, which is less than 3002- 3000 = 2, so it must go.queue10030013002frontbackAfter popping 1 and pushing 3002, the queue holdsexactly the requests in [2, 3002].push twhile front < t - 3000: pop frontreturn len(queue)
Push the new time, pop from the front while it is too old, then the queue size is the answer.
ComplexityTime O(1) amortized per pingSpace O(w), where w is the number of rEach timestamp is pushed once and popped at most once, so the total work across all pings is linear in the number of calls.

What Trips People Up in Real Interviews

1

Clarify that the time range is inclusive on both ends

2

Explain the sliding window approach removes expired entries on each ping

3

Mention that a simple list with a pointer also works but queue is cleaner

4

Discuss that the queue never grows beyond 3000 entries (bounded window)

5

Note the amortized O(1) complexity since each ping is added and removed exactly once

Solution Code

O(1) amortized per ping time
from collections import deque

class RecentCounter:
    def __init__(self):
        self.queue = deque()

    def ping(self, t: int) -> int:
        self.queue.append(t)
        while self.queue[0] < t - 3000:
            self.queue.popleft()
        return len(self.queue)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Number of Recent Calls problem?

Implement a class that tracks incoming requests with timestamps. The ping(t) method returns the number of recent calls in the time range [t-3000, t]. This is a classic sliding window problem using a queue to maintain the window of recent events.

How do you solve Number of Recent Calls?

The optimal approach runs in O(1) amortized per ping time and O(w), where w is the number of r space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Number of Recent Calls?

Number of Recent Calls is asked at Databricks. It is a easy difficulty problem.

What are common mistakes on Number of Recent Calls?
  • Clarify that the time range is inclusive on both ends
  • Explain the sliding window approach removes expired entries on each ping
  • Mention that a simple list with a pointer also works but queue is cleaner
  • Discuss that the queue never grows beyond 3000 entries (bounded window)
  • Note the amortized O(1) complexity since each ping is added and removed exactly once