PythonStudio
Interview Problems

Token Bucket Rate Limiter

medium · fake clock

Given capacity >= 1, refill >= 0 tokens per integer time unit, and requests in nondecreasing time order, return one admission boolean per request. The bucket starts full at time 0. Refill up to capacity according to elapsed time before each request; an admitted request costs one token. Rejected requests cost nothing. All values are integers, repeated timestamps are valid, and time never goes backward. Do not read a real clock or sleep. Examples (JSON; null means Python None): Input: {"capacity": 2, "refill": 1, "requests": [0, 0, 0, 1]} Output: [true, true, false, true] Input: {"capacity": 1, "refill": 0, "requests": [0, 100]} Output: [true, false]

Starter code

def token_bucket(capacity: int, refill: int, requests: list[int]) -> list[bool]:
    """Admit requests using a deterministic replenishing token balance."""
    pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler