← All Exercises
Fixed-Window Rate Limiter
basicjavascript
Background
Fixed windows are the cheapest throttle to reason about: one counter per window. Their weakness is boundary bursts, which sliding windows and token buckets address at higher cost.
Problem
Implement a fixed-window rate limiter. Each request carries an id and a timestamp. The window index is floor(timestamp / windowSec); a counter per window increments on every request, and only the first limit requests in a window are allowed. Output the ids of allowed requests in order, then the rejected ids.
Input
A JSON object {"limit": number, "windowSec": number, "requests": [{"t": number, "id": string}]} with non-decreasing timestamps.Output
A JSON object {"allowed": string[], "rejected": string[]} preserving arrival order within each list.Sample tests
Sample 1
Input: {"limit":2,"windowSec":10,"requests":[{"t":0,"id":"a"},{"t":1,"id":"b"},{"t":2,"id":"c"},{"t":10,"id":"d"},{"t":10,"id":"e"},{"t":11,"id":"f"}]}Expected: {"allowed":["a","b","d","e"],"rejected":["c","f"]}Sample 2
Input: {"limit":3,"windowSec":60,"requests":[{"t":59,"id":"x"},{"t":60,"id":"y"},{"t":61,"id":"z"}]}Expected: {"allowed":["x","y","z"],"rejected":[]}Your solution
Learning objective
Model window keying and per-window counters, and see the boundary-burst weakness firsthand.