The problem
Implement ping(t): record a request at time t and return how many requests occurred in [t − 3000, t]. Times strictly increase.
Stuck? Reveal hints one at a time
How to approach it
- 1Append t to a queue.
- 2Pop from the front while front < t − 3000.
- 3Return the queue length.
Key insight
Because timestamps increase, once a request falls out of the window it can never return — each request is enqueued and dequeued at most once, giving amortized O(1).
The solution
Watch out for
- The window is INCLUSIVE at t − 3000 — evict with <, not ≤.
- In JS, Array.shift() is O(n); use a head index or a real deque.