Per-user token bucket limiter with interval refills
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in SpaceXAI's onsite applied coding round, last asked Apr 2026. It tests rate limiting, object-oriented design and hash map.
Build a per-user token rate limiter for an AI API. Every user has their own policy, and one user's traffic never affects another's.
A policy has three positive integers:
-
max_tokens: the bucket's capacity. A new user starts with a full bucket. -
refill_amount: how many tokens one refill adds. -
interval: seconds between refills. Refills happen in whole steps: after each fullintervalsince the user's last refill,refill_amounttokens are added, never exceedingmax_tokens.
Requests arrive at irregular times, and a user may go idle for a long time. Do not use a background timer.
Implement TokenLimiter:
-
set_policy(user_id, max_tokens, refill_amount, interval, timestamp)creates or replaces a user's policy. Replacing a policy must not give the user extra tokens. -
consume(user_id, tokens, timestamp) -> boolspendstokensif the user has at least that many attimestampand returnsTrue. Otherwise it spends nothing and returnsFalse. -
get_tokens(user_id, timestamp) -> intreturns the user's balance attimestamp.
Timestamps are integer seconds and are usually non-decreasing. A late (smaller) timestamp must never add tokens. Calling consume or get_tokens for a user without a policy is an error.
Example
Policy for alice: max_tokens=10, refill_amount=3, interval=5, set at t=0.
| Call | Result | Why |
|---|---|---|
consume("alice", 8, 1) |
True |
bucket starts full at 10, now 2 |
consume("alice", 3, 4) |
False |
no refill yet, 2 < 3 |
get_tokens("alice", 5) |
5 |
first refill at t=5 adds 3 |
get_tokens("alice", 100) |
10 |
many refills, capped at capacity |
Example 2
Policy for bob: max_tokens=4, refill_amount=1, interval=2 at t=0. consume("bob", 4, 0) returns True, consume("bob", 1, 1) returns False, and consume("bob", 1, 2) returns True.
Aim for O(1) time per call.
Clarifying questions
- After a refill hits the cap, does the refill clock still advance in whole intervals from the last refill, or reset to the time it filled?
- When a policy is replaced, is the balance kept (capped at the new
max_tokens) and is the refill clock kept or restarted attimestamp? - Can
consumeask for more tokens thanmax_tokens, or for zero? - What does a late timestamp do to
consume: is the balance as of the latest time seen?
What a strong answer covers
- Per user: the policy, a balance and
last_refill; no timers. On each call,steps = (timestamp - last_refill) // interval, then addsteps · refill_amountcapped atmax_tokensand movelast_refillforward bysteps · interval, keeping the refill phase. -
O(1)per call regardless of idle time, since steps are computed, not looped. - Late timestamps:
stepsis clamped at zero andlast_refillnever moves back, so they can't add tokens. -
set_policyon an existing user keeping the current balance capped at the new maximum, so replacing a policy never gives extra tokens; a new user starting full. - Tests: both examples, a refill exactly at a multiple of
interval, a long idle period, a late timestamp, a policy change that lowers and one that raises the cap, and an error for a user without a policy.
Follow-ups
- Make it safe for many threads, with one user's lock never blocking another.
- Run it across many API servers that share limits (Redis, a script per check).
- Charge by model tokens after the response, when the cost isn't known up front.
- Evict idle users to bound memory.
Where it's asked
- SpaceXAI · Onsite applied coding · Last asked Apr 2026Two or three rounds, in person (Palo Alto for many roles)
What it tests
rate limiting · object-oriented design · hash map
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
