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
LLM API Rate Limiting
00:00
5 left

LLM API Rate Limiting

MediumPython

Problem

Implement a rate limiter for the Axis Max Life Insurance policy-assistance LLM endpoint. Each user may make at most limit accepted requests during any rolling interval of window seconds.

Process requests in the order received. For each request, remove that user's accepted request timestamps that are outside the current window. Accept the request if fewer than limit accepted requests remain, otherwise reject it. Rejected requests do not consume quota.

Formal Specification

Implement rate_limit(requests, limit, window).

  • requests is a list of two-element lists, [user_id, timestamp], ordered by nondecreasing timestamp.
  • user_id is a string.
  • timestamp, limit, and window are positive integers.
  • Return a list of booleans in request order. True means accepted and False means rejected.
  • A request at time t considers timestamps in the half-open interval [t - window, t) expired. A request at exactly t - window is therefore expired.

Constraints

  • 1 <= len(requests) <= 2 * 10^5
  • 1 <= limit, window <= 10^9
  • 0 <= timestamp <= 10^9
  • user_id is a non-empty string
  • Requests are ordered by nondecreasing timestamp

Function Signature

def rate_limit(requests, limit, window):
Interviewer

Your question is LLM 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.