Token bucket with refill
Simulate a token-bucket rate limiter. The bucket starts full with `capacity` tokens and refills at `refill_rate` tokens per time unit, never exceeding `capacity`. Given a chronologically ordered list of `[timestamp, cost]` requests, each request first refills the bucket based on elapsed time since the previous request, then is allowed (consuming `cost` tokens) only if at least `cost` tokens are available. Return a list of booleans for the requests. Timestamps are non-decreasing.
Implement
token_bucket(capacity: int, refill_rate: int, requests: list[list]) → list[bool]Examples
in
[2,1,[[0,1],[0,1],[0,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 25 min
solution.py
InputExpectedGot
[2,1,[[0,1],[0,1],[0,1]]][true,true,false]not run yetsample