Number of Recent Calls

Easy · Queue & Deque · about 10 minutes

Asked at Google, Amazon, Stripe, Affirm.

Implement RecentCounter, which tracks requests within a sliding 3-second window. ping(t) logs a request at time t (milliseconds) and returns how many logged requests fall between t - 3000 and t, both ends included. Each call's t is strictly greater than the previous call's.

Related guides