Code Room
CodingHardcod-g224
Subject Rate limiter designLevel Senior–Staff~35 minCommon in Networking & APIs interviewsIndustries Software development

Question

Simulate a token-bucket rate limiter. The bucket has integer 'capacity' and refills at 'rate' tokens per second (a float allowed via the formula below). Given a list of request timestamps (floats, non-decreasing seconds), process them in order. At each request, first refill: add rate*(now - last_refill_time) tokens, capped at capacity, then update last_refill_time to now. The bucket starts full at the time of the first request. A request is allowed if at least 1 token is available, consuming 1 token; otherwise it is rejected. Return a list of booleans, one per request, True=allowed. If there are no requests return an empty list.

Implement
token_bucket(timestamps: list[float], capacity: int, rate: float) → 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.