Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

API Rate Limiter Function

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

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

Oracle Health APIs need a rate limiter that enforces both a per-client quota and a per-route quota. Process requests in arrival order and accept a request only when adding it would keep both quotas within their limits.

Use an exact sliding window. For a request at timestamp t, only previously accepted requests with timestamps greater than t - window count. Rejected requests do not consume quota.

Formal Specification

Implement rate_limit(requests, client_limit, route_limit, window). requests is a list of records, where each record is [client_id, route, timestamp]. client_id and route are strings, and timestamp is an integer. Return a list of booleans in the same order, where True means accepted and False means rejected.

Timestamps are nondecreasing across the input. The client quota counts accepted requests from that client across all routes. The route quota counts accepted requests to that route across all clients.

Constraints

  • 1 <= len(requests) <= 10^5
  • 1 <= client_limit, route_limit, window <= 10^9
  • Each request is [client_id, route, timestamp]
  • Timestamps are nondecreasing
  • Identifiers are non-empty strings

Function Signature

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