Code Room
CodingMediumcod-g447
Subject Rate limiter designLevel Mid–Senior~30 minCommon in Networking & APIs interviewsIndustries Software development

Question

Simulate a token-bucket rate limiter using integer arithmetic only. The bucket holds up to `capacity` tokens and starts full. It refills at `rate` tokens per second, accrued continuously: between two times, floor((t - last_refill) * rate) tokens are added (capped at capacity), and `last_refill` advances by the whole seconds actually consumed for those tokens to avoid losing fractional credit. Each request (given as non-decreasing integer timestamps) costs 1 token: if a token is available it is ALLOWED and a token is spent, else REJECTED. Return the list of booleans. Assume rate >= 1 and capacity >= 1.

Implement
token_bucket(requests: list[int], capacity: int, rate: int) → list[bool]
Examples
in[[0,0,0],2,1]out[true,true,false]
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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.