Number of Recent Calls - Problem

You have a RecentCounter class which counts the number of recent requests within a certain time frame.

Implement the RecentCounter class:

  • RecentCounter() Initializes the counter with zero recent requests.
  • int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t].

It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.

Input & Output

Example 1 — Basic Usage
$ Input: operations = ["RecentCounter", "ping", "ping", "ping", "ping"], values = [null, 1, 100, 3001, 3002]
Output: [null, 1, 2, 3, 3]
💡 Note: RecentCounter() creates counter. ping(1) returns 1 (only request 1). ping(100) returns 2 (requests 1,100). ping(3001) returns 3 (requests 1,100,3001). ping(3002) returns 3 (requests 100,3001,3002 - request 1 expired)
Example 2 — All Requests Expire
$ Input: operations = ["RecentCounter", "ping", "ping"], values = [null, 1, 5000]
Output: [null, 1, 1]
💡 Note: ping(1) returns 1. ping(5000) returns 1 because request at time 1 is outside range [5000-3000, 5000] = [2000, 5000]
Example 3 — Close Timestamps
$ Input: operations = ["RecentCounter", "ping", "ping", "ping"], values = [null, 1000, 2000, 3500]
Output: [null, 1, 2, 3]
💡 Note: All requests stay within 3000ms window: ping(3500) checks range [500, 3500] which includes 1000, 2000, 3500

Constraints

  • 1 ≤ t ≤ 109
  • Each test case will call ping with strictly increasing values of t
  • At most 104 calls will be made to ping

Visualization

Tap to expand
Number of Recent Calls Queue-Based Solution INPUT Operations: RecentCounter ping ping ping ping Values (time t in ms): null 1 100 3001 3002 Time Window: 3000ms 1 100 3001 3002 Range: [t-3000, t] Count requests in window Queue Structure: front back ALGORITHM STEPS 1 Initialize Queue Create empty queue to store request times 2 Add New Request Push time t to back of the queue 3 Remove Old Requests Pop from front while front value < t - 3000 4 Return Queue Size Queue size = count of valid requests Queue States: ping(1): [1] --> 1 ping(100): [1,100] --> 2 ping(3001): [1,100,3001] --> 3 ping(3002): [100,3001,3002] (removed 1) --> 3 FINAL RESULT Output Array: null 1 2 3 3 Step-by-Step Results: RecentCounter() --> null Initialize empty queue ping(1) --> 1 request in [-2999, 1] Queue: [1], Size: 1 ping(100) --> 2 in [-2900, 100] Queue: [1, 100], Size: 2 ping(3001) --> 3 in [1, 3001] Queue: [1, 100, 3001], Size: 3 ping(3002) --> 3 in [2, 3002] Remove t=1 (out of range) Queue: [100, 3001, 3002], Size: 3 OK - Complete Key Insight: The queue maintains requests in chronological order. Since ping() is called with strictly increasing time values, we only need to remove old requests from the front. This gives us O(1) amortized time complexity per ping call, as each request is added and removed at most once. TutorialsPoint - Number of Recent Calls | Queue-Based Solution
Asked in
Google 25 Amazon 20 Microsoft 15
95.0K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen