Problem, as an interviewer would state it: "Implement a rate limiter using the token bucket algorithm — a bucket holds up to N tokens, each request consumes one, and tokens refill continuously over time. Show me it correctly rejects requests once the bucket is empty, and correctly allows more after tokens refill."
Examples:
const limiter = createTokenBucket({ capacity: 3, refillRatePerSec: 10 });
limiter.tryConsume(); // true, true, true, then false, false for 5 rapid calls
Clarifying questions expected:
- Should tokens refill continuously (fractional tokens accumulating smoothly) or in discrete steps?
- Can a single request cost more than one token?
- Is this rate limiter meant to run per-client (one bucket each) or globally (one shared bucket)?
Code / implementation expected: Yes — real, direct proof that exactly the bucket's own capacity worth of rapid requests are allowed, the rest genuinely rejected, and more become available after a real, measured refill period.