Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Implement Exact Sliding Window Rate Limiting
00:00
5 left

Implement Exact Sliding Window Rate Limiting

HardPython

Problem

A healthcare service 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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.