Number of Recent Calls
Module 9 · Queues
Problem
Implement RecentCounter.ping(t): each call records a request at time
t (milliseconds) and returns how many requests occurred in the window
[t − 3000, t]. Successive calls use strictly increasing t.
Examples
Example 1
Input
ping(1)Output1Explanation. window [-2999, 1], requests {1}
Example 2
Input
ping(100)Output2Explanation. window [-2900, 100], requests {1, 100}
Example 3
Input
ping(3001)Output3Explanation. window [1, 3001], requests {1, 100, 3001}
Example 4
Input
ping(3002)Output3Explanation. window [2, 3002], requests {100, 3001, 3002}; 1 expired
Constraints
≤ 10⁴ calls · t strictly increasing.
Attempt it first
The module's warm-up: a real rate-limiter's counting half. The strictly-increasing guarantee is doing quiet, heavy work — identify what it buys before opening the hint.