Code RoomToken bucket with fractional refill
MediumPrep Room Coding #1527

Token bucket with fractional refill

CodingStorage & CDNNetworking & APIsMid–Senior~25 min

Implement a token-bucket rate limiter. The bucket holds at most `capacity` tokens and refills at `refill_rate` tokens per second (fractional accrual allowed). It starts full at capacity. Process a list of request events as [timestamp, cost] pairs with non-decreasing integer timestamps. Before each request, add (timestamp - last_timestamp) * refill_rate tokens, clamped at capacity. If the bucket holds at least `cost` tokens, allow the request and subtract `cost`; otherwise deny it and leave the bucket unchanged. The first event uses the bucket's initial full state with no prior refill. Return a list of booleans: True if allowed, False if denied. capacity and refill_rate are positive numbers.

Implement
token_bucket(capacity: float, refill_rate: float, events: list[list]) → list[bool]
Examples
in[5,1,[[0,3],[0,3],[2,2]]]out[true,false,true]
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 25 min
InputExpectedGot
[5,1,[[0,3],[0,3],[2,2]]][true,false,true]not run yetsample