Code RoomSliding-window rate limiter
MediumPrep Room Coding #1560

Sliding-window rate limiter

CodingSecurityNetworking & APIsMid–Senior~18 min

Implement a sliding-window rate-limiter check for an API gateway. Given a list of request `timestamps` (integers) already recorded for a user, a `window` length, a `limit` (max allowed requests within the window), and the current time `now`, decide whether a NEW request at `now` is allowed. A request counts as in-window if its timestamp is strictly greater than `now - window`. The new request is allowed only if the number of in-window prior requests is strictly less than `limit`. Return True if allowed.

Implement
rate_limit_allowed(timestamps: list[int], window: int, limit: int, now: int) → bool
Examples
in[[1,2,3],10,5,10]outtrue
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 18 min
InputExpectedGot
[[1,2,3],10,5,10]truenot run yetsample