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
Secure Token Bucket Rate Limiter
00:00
5 left

Secure Token Bucket Rate Limiter

HardPython

Problem

Meta Graph API services often enforce per-client request limits using a token bucket. Implement a secure in-memory rate limiter that validates a request signature and then decides whether the request is allowed under a per-key token bucket.

Write a function that processes a list of request events in timestamp order. Each event contains timestamp, key, cost, and signature. A request is valid only if its signature matches the expected signature for that key. Invalid signatures must be rejected and must not consume tokens. Valid requests consume cost tokens only if enough tokens are available at that timestamp.

Formal Specification

Input:

  • capacity: integer bucket size
  • refill_rate: integer tokens added per second
  • secrets: map from string key to string secret
  • requests: list of [timestamp, key, cost, signature]

Output:

  • Return a list of booleans where each value indicates whether the corresponding request is allowed.

The expected signature for a request is the string: key + ":" + str(timestamp) + ":" + str(cost) + ":" + secret

Each key has its own bucket. Buckets start full at capacity. On each request, refill based on elapsed whole or fractional seconds since that key's previous valid request processing time, capped at capacity.

Constraints

  • 1 <= capacity <= 10^6
  • 1 <= refill_rate <= 10^6
  • 1 <= len(requests) <= 10^5
  • 0 <= timestamp <= 10^9
  • 1 <= cost <= 10^6
  • Requests are sorted in non-decreasing timestamp order
  • Each request is formatted as [timestamp, key, cost, signature]
  • If a key is not present in secrets, all requests for that key are invalid

Function Signature

def secure_token_bucket(capacity, refill_rate, secrets, requests):
Interviewer

Your question is Secure Token Bucket Rate Limiter. 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.