Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rate Limiter Class

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

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

Grammarly services need to prevent a client from sending too many requests in a short period. Implement a rate limiter that processes request timestamps in chronological order and determines whether each request is accepted.

A request at time t is accepted when fewer than limit accepted requests occurred in the half-open interval (t - 1, t] before it is evaluated. Rejected requests do not consume capacity. Exactly one second-old requests are outside the window and must be removed before evaluating the current request.

Expose the algorithm through simulate_rate_limiter(timestamps, limit), which models a stateful RateLimiter class and returns one Boolean decision per timestamp.

Formal Specification

  • Input: timestamps, a nondecreasing list of numbers representing request times in seconds, and positive integer limit.
  • Output: A list of Booleans. The value at index i is True if the request at timestamps[i] is accepted, otherwise False.

Constraints

  • 0 <= len(timestamps) <= 10^5
  • 0 <= timestamps[i] <= 10^12
  • timestamps is nondecreasing
  • 1 <= limit <= 10^5
  • Timestamps may be integers or floating-point values

Function Signature

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