Question presented to candidate: "You need to limit each API key to 10 requests per minute. Walk me through fixed window, sliding window and token bucket. What does each one let through in a burst, how much state does it keep per key, and which one would you pick?"
What a strong answer should cover:
- Fixed window counter: one counter per key per clock-aligned window; tiny state, but a boundary burst lets through up to 2x the limit within a few seconds across a window edge
- Sliding window log: store every accepted timestamp, count the last W seconds; exact, but memory grows with the limit (one entry per request in the window)
- Sliding window counter: weight the previous window count by its overlap; a few numbers of state, close to exact, but an approximation that can over- or under-admit when traffic inside a window is uneven
- Token bucket: capacity (burst) plus refill rate; allows a controlled burst up to capacity and then the steady rate; two numbers of state; the usual default for APIs. Leaky bucket paces output at a constant rate
- Choosing: token bucket for APIs that should tolerate short bursts; sliding log where exactness matters and limits are small; sliding counter at very large scale; fixed window only when the boundary burst is acceptable
- Know the parameters: a token bucket "10/min" with capacity 10 admits more than 10 in the first minute
Code / implementation expected: Yes — implementing at least one algorithm; the example runs all four on the same traffic in four languages.