Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

API Rate Limiter Implementation

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

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

DoorDash Drive API requests must be limited independently for each client. Given a chronologically ordered stream of requests, determine whether each request should be accepted under a sliding-window rate limit.

A request is identified by its integer timestamp and client ID. A request is accepted when that client has made fewer than limit accepted requests during the current window seconds, including the current timestamp. Rejected requests do not consume quota.

Formal Specification

Implement rate_limit(requests, limit, window), where requests is a list of two-element lists [timestamp, client_id]. Return a Boolean list whose element at index i indicates whether request i is accepted. Timestamps are nondecreasing, limit is positive, and window is a positive integer. A request at time t counts requests with timestamps in [t - window + 1, t].

Constraints

  • 0 <= requests.length <= 10^5
  • 0 <= timestamp <= 10^9
  • 1 <= limit <= 10^5
  • 1 <= window <= 10^9
  • Client IDs are non-empty strings
  • Requests are ordered by nondecreasing timestamp

Function Signature

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