Problem
Design a class that counts the recent requests in a window of time.
Write the class RecentCounter with these operations:
RecentCounter() creates a counter with no requests. ping(t) adds a new request at time t, in milliseconds. Then it returns the number of requests in the time range [t - 3000, t].
The range includes both ends, so the new request is always in it. Each call to ping uses a larger t than the call before it.
Example 1
- Input:
- ["RecentCounter","ping","ping","ping","ping"] [[],[5],[2000],[3005],[3010]]
- Output:
- [null,1,2,3,3]
- Explanation:
- At time 3005, the range
[5, 3005]includes the request at time 5. At time 3010, the range[10, 3010]does not include it.
Example 2
- Input:
- ["RecentCounter","ping","ping"] [[],[100],[5000]]
- Output:
- [null,1,1]
- Explanation:
- The range for the second call is
[2000, 5000]. The first request is not in this range.
Constraints
- 1 <= t <= 10^9
- Each call to ping uses a strictly larger t than the call before it.
- At most 10^4 calls to ping
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 933. Number of Recent Calls
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source