KatInterview

Per-Key Token Bucket Admission Decisions

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designrate-limitertoken-buckethash-map

Last updated

Simulate independent token buckets per key over a request log, with exact fractional refill, capped capacity, and out-of-order timestamps, to decide which requests are admitted.

You are given a log of requests hitting a rate limiter. The limiter keeps one independent token bucket for every distinct key. Process the log in the order given and report, for each request, whether it is admitted.

Bucket rules

  • Every bucket holds at most capacity tokens.
  • A key's bucket is created when that key first appears in the log. It starts full (capacity tokens) as of that first request's timestamp.
  • Tokens are added continuously at refill_rate tokens per second. Refill is exact and fractional: after 500 ms at a rate of 2 tokens per second, the bucket gains exactly 1 token, and after 1 ms at a rate of 1 token per second it gains exactly 0.001 tokens. Nothing is rounded. A bucket never exceeds capacity.
  • Each request has a cost. Before deciding, bring the bucket up to date to the request's timestamp (add the refill earned since the bucket's last update, capped at capacity). Then:
    • If the bucket holds at least cost tokens, the request is admitted and cost tokens are removed.
    • Otherwise it is rejected and the bucket loses no tokens. The refill earned up to this request's timestamp is still kept.
  • A request whose cost exceeds capacity can never be admitted.

Time handling

Timestamps are in milliseconds. The log is processed in the given order but timestamps are not guaranteed to be non-decreasing for a key. If a request's timestamp is earlier than the latest timestamp already seen for its key, it earns no refill and the bucket's "last updated" time does not move backward. It is still judged against the current token balance.

Task

Implement token_bucket_decisions(capacity, refill_rate, requests).

  • capacity: maximum tokens in each bucket.
  • refill_rate: tokens added per second.
  • requests: list of [key, timestamp_ms, cost] entries in processing order.

Return a list of booleans, one per request in the same order, where True means admitted.

Constraints

  • capacity and refill_rate are non-negative integers.
  • cost and timestamp_ms are integers; cost >= 0 and timestamp_ms >= 0.
  • key is a hashable identifier (for example an integer); different keys never affect each other.
  • Up to about 10^5 requests.
  • The output must contain exactly one boolean per request.

Examples

Example 1

Input: token_bucket_decisions(5, 2, [[1, 0, 1], [1, 0, 4], [1, 0, 1], [1, 500, 1], [1, 1000, 1], [2, 1000, 5]])

Output: [True, True, False, True, True, True]

Example 2

Input: token_bucket_decisions(2, 1, [[1, 0, 2], [1, 999, 1], [1, 1000, 1]])

Output: [True, False, True]

Hints

Hint 1

Keep a dictionary from key to its current balance and the timestamp of its last update. Create the entry lazily on the key's first request, with a full bucket.

Hint 2

Avoid floating point. Since 1 token per second equals 1 thousandth of a token per millisecond, store balances in thousandths of a token and add `elapsed_ms * refill_rate` on each update.

Hint 3

On every request, refill first (cap at capacity), then compare to the cost. Rejected requests must still persist the refilled balance. For out-of-order timestamps, clamp elapsed time at 0 and never move the stored time backward.