Code RoomToken bucket rate limiter
MediumPrep Room Coding #158

Token bucket rate limiter

CodingSecurityNetworking & APIsMid–Senior~20 min

Implement a token-bucket rate-limit check. The bucket has a `capacity` (max tokens), a `refill_rate` (tokens added per unit time), a current `tokens` count, the `last_refill` time, the current time `now`, and a request `cost`. First refill: add (now - last_refill) * refill_rate tokens, capped at capacity. Then if the bucket has at least `cost` tokens, the request is allowed: deduct the cost. Return a 2-element list [allowed, remaining_tokens] where allowed is a bool and remaining_tokens is the token count after the (possible) deduction. If not allowed, tokens are unchanged (after refill).

Implement
token_bucket_allow(capacity: int, refill_rate: int, tokens: int, last_refill: int, now: int, cost: int) → list
Examples
in[10,1,5,0,3,2]out[true,6]
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 20 min
InputExpectedGot
[10,1,5,0,3,2][true,6]not run yetsample