← Back to problems

8. Sliding-Window Rate Limiter

HARD
DESIGNLLDRate LimiterSliding WindowSystem Design

Implement a per-client sliding-window-log rate limiter that allows at most N requests in any trailing window of width W.

Implement a rate limiter using the sliding-window-log algorithm, tracked independently per client. Configuration: at most N requests are permitted within any trailing time window of width W. The window is half-open: a request at time t is evaluated against all previously ALLOWED requests whose timestamps fall in the interval (t - W, t] : strictly greater than t - W, and less than or equal to t. Rules: - A request at time t is ALLOWED if and only if, counting this request itself, the number of that client's allowed requests with timestamps in (t - W, t] is at most N. Otherwise it is DENIED. - Only ALLOWED requests are recorded. A DENIED request does not consume a slot and is never stored, so it has no effect on future decisions. - Each client is limited independently; one client's traffic never affects another's decisions. - Timestamps are non-decreasing across the whole command stream. Multiple requests may share a timestamp. Commands: - INIT <N> <W> : first line; limit N requests per trailing window of width W. - REQ <clientId> <t> : a request from <clientId> at time t; print "ALLOW" or "DENY" and, if allowed, record it. - COUNT <clientId> <t> : read-only; print how many of that client's allowed requests fall in the trailing window (t - W, t] as of time t. This never records a request and never changes state.
Log in to submit a solution

Comments

Log into join the discussion.