Practice

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

Inputping(1)Output1

Explanation. window [-2999, 1], requests {1}

Example 2

Inputping(100)Output2

Explanation. window [-2900, 100], requests {1, 100}

Example 3

Inputping(3001)Output3

Explanation. window [1, 3001], requests {1, 100, 3001}

Example 4

Inputping(3002)Output3

Explanation. 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.