Code RoomToken bucket limiter
MediumPrep Room Coding #1016

Token bucket limiter

CodingNetworking & APIsMid–Senior~30 min

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.

0:00 of about 30 min
InputExpectedGot
[[0,0,0],2,1][true,true,false]not run yetsample