Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Leaky Rate Limiter

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

Your question is Leaky Rate Limiter. 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

Box API traffic must be throttled so bursts do not overwhelm downstream services. Implement a leaky bucket rate limiter that processes request timestamps in chronological order and determines whether each request is accepted or rejected.

The bucket has a maximum capacity measured in requests. Requests enter the bucket one at a time. Between two request timestamps, the bucket leaks at a constant rate. A request is accepted only if adding it would not make the current bucket level exceed capacity; otherwise, it is rejected. Rejected requests do not increase the bucket level, but time continues to pass and the bucket continues to drain.

Formal Specification

Implement rejected_requests(timestamps, capacity, leak_rate). timestamps is a nondecreasing list of integers representing seconds since an arbitrary origin. capacity and leak_rate are positive integers. Return a Boolean list where element i is True if the request at timestamps[i] is rejected, and False otherwise.

The bucket starts empty. At time t, first subtract leak_rate * (t - previous_time) from the bucket level, never allowing the level below zero.

Constraints

  • 0 <= len(timestamps) <= 10^5
  • 0 <= timestamps[i] <= 10^9
  • timestamps is nondecreasing
  • 1 <= capacity <= 10^9
  • 1 <= leak_rate <= 10^9

Function Signature

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