Your question is Token Bucket Rate Limiting. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Implement a basic rate-limiting algorithm, such as a token bucket, to protect an API endpoint from denial-of-service attacks.
Implement token_bucket(requests, capacity, refill_rate), where requests is a nondecreasing list of timestamps in seconds, capacity is the maximum token count, and refill_rate is tokens added per second. Start with a full bucket and return a Boolean list indicating whether each request is accepted. Each accepted request consumes one token; tokens refill continuously up to capacity before each request.
Constraints: 0 <= len(requests) <= 1000, 1 <= capacity <= 10^6, refill_rate > 0, and timestamps are nondecreasing.
def token_bucket(requests, capacity, refill_rate):