Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Token Bucket Rate Limiting

HardPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Constraints

  • 0 <= len(requests) <= 1000
  • 1 <= capacity <= 10^6
  • refill_rate > 0
  • requests contains numeric timestamps in seconds
  • requests is sorted in nondecreasing order

Function Signature

def token_bucket(requests, capacity, refill_rate):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output