Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
API Rate Limiting
00:00
5 left

API Rate Limiting

HardPython

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):
Interviewer

Your question is API Rate Limiting. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.