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
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
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]][null, 1, 2, 3, 3]["RecentCounter", "ping", "ping"]
[[], [5], [3000]][null, 1, 2]["RecentCounter", "ping", "ping"]
[[], [1], [3002]][null, 1, 1]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Databricks | Easy | View all Databricks questions → |
How to Think About It
Store all ping timestamps in a list or queue
For each new ping, remove all timestamps older than t-3000
Count the remaining timestamps in the window [t-3000, t]
A deque or queue is ideal for efficient removal from the front
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
What Trips People Up in Real Interviews
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
Solution Code
O(1) amortized per ping timefrom 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)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