Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

API Rate Limiting

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

Your question is API 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

Amperos Health uses an API for patient-record access. Implement an exact sliding-window rate limiter that enforces both a per-client limit and a global per-endpoint limit.

Each request is represented as [timestamp, client_id, endpoint]. Requests may be provided out of chronological order, but requests with equal timestamps must be processed in their original input order. A request is allowed only if accepting it would keep both limits within their windows. Rejected requests do not count toward either limit. Return decisions in the original input order.

Formal Specification

Implement rate_limit(requests, per_client_limit, global_limit, window), where requests is a list of requests, timestamps and limits are integers, and client IDs and endpoints are strings. Return a Boolean list with one value per request. For a request at time t, only previously accepted requests with timestamps strictly greater than t - window count. The current request counts immediately if it is accepted.

Constraints

  • 1 <= len(requests) <= 2 * 10^5
  • Each request is [timestamp, client_id, endpoint]
  • Timestamps may be unsorted
  • Equal timestamps retain original input order
  • 1 <= per_client_limit, global_limit, window <= 10^9
  • Only accepted requests count toward either limit

Function Signature

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