← Questions

Per-user token bucket limiter with interval refills

0
SpaceXAICodingLast asked Apr 2026

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 full interval since the user's last refill, refill_amount tokens are added, never exceeding max_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) -> bool spends tokens if the user has at least that many at timestamp and returns True. Otherwise it spends nothing and returns False.
  • get_tokens(user_id, timestamp) -> int returns the user's balance at timestamp.

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 at timestamp?
  • Can consume ask for more tokens than max_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 add steps · refill_amount capped at max_tokens and move last_refill forward by steps · interval, keeping the refill phase.
  • O(1) per call regardless of idle time, since steps are computed, not looped.
  • Late timestamps: steps is clamped at zero and last_refill never moves back, so they can't add tokens.
  • set_policy on 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

What it tests

rate limiting · object-oriented design · hash map

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding