Token bucket rate limiter
Simulate a token-bucket rate limiter. The bucket holds at most 'burst' tokens and refills at 'rate' tokens per second, capped at burst. Requests arrive at the given non-decreasing integer 'timestamps' (seconds). The bucket starts full. For each request, first refill by elapsed*rate since the previous request (clamped to burst), then admit the request if at least one token is available (consuming one) else deny. Return a list of booleans, one per request. An empty timestamp list returns an empty list.
Implement
token_bucket(rate: int, burst: int, timestamps: list[int]) → list[bool]Examples
in
[1,2,[0,0,0]]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 22 min
solution.py
InputExpectedGot
[1,2,[0,0,0]][true,true,false]not run yetsample