Map Tile Burst Allowance

MediumdesignAlso known as Token Bucket Rate Limiter

A map company sells tile access to app developers. Panning a map needs a burst of tiles, then nothing for a while, so each API key gets a token bucket instead of a fixed per-minute cap. The bucket holds up to capacity tokens, a request spends tokens, and one token drips in every intervalMs milliseconds. A full bucket allows a burst, and the drip rate sets the long-run rate.

This is the token bucket from Rate Limiting (see also Design a Rate Limiter). No timer refills the bucket. Instead, each request works out how many tokens have arrived since the last one, and that calculation is where most bugs occur.

Your task

Write a class TokenBucket:

  • constructor(capacity, intervalMs) creates a bucket that starts full at time 0.
  • take(t, cost) handles a request at time t (milliseconds) needing cost tokens. If the bucket holds at least cost, remove them and return true. Otherwise return false and remove nothing.
  • available(t) returns how many tokens the bucket holds at time t and changes nothing else.

Drips happen at fixed times: intervalMs, 2 × intervalMs, 3 × intervalMs, and so on. Each adds one token, unless the bucket is full, in which case the token is lost. A drip at exactly time t counts before the request at t.

class TokenBucket

Examples

Example 1
Input
new TokenBucket(3, 1000)
.take(0, 1)
.take(100, 1)
.take(200, 1)
.take(300, 1)
.take(1000, 1)
.take(1500, 1)
.available(2000)
Output
[null, true, true, true, false, true, false, 1]
Why

The burst at 0, 100 and 200 ms empties the 3 tokens. At 300 ms nothing has dripped yet, so the request is rejected. The drip at 1,000 ms adds a token, which the request at 1,000 spends. At 1,500 ms the bucket is empty again, so that request is rejected, and the drip at 2,000 ms leaves 1 token.

Example 2
Input
new TokenBucket(2, 1000)
.take(0, 2)
.take(600, 1)
.take(1200, 1)
.take(1900, 1)
.take(2000, 1)
Output
[null, true, false, true, false, true]
Why

A cost-2 request empties the bucket at 0. At 600 ms no drip has happened. By 1,200 ms the drip at 1,000 has added a token, so the request is allowed; measuring time "since the last request" would wrongly reject it. The drip at 2,000 ms supplies the token for the last request.

Constraints

  • 1 ≤ capacity ≤ 1,000,000
  • 1 ≤ intervalMs ≤ 1,000,000
  • 1 ≤ cost ≤ 1,000,000 (a cost above capacity can never succeed)
  • 0 ≤ t ≤ 1,000,000,000,000, and t never decreases from one call to the next
  • At most 2,000 calls per test
Start with brute force

A gateway with a million API keys cannot run a million refill timers. Simulating each drip inside every call is also too slow, because a 1 ms interval with calls a trillion milliseconds apart takes a trillion steps.

One division counts the drips in O(1), and the bucket is two numbers per key, small enough for a shared in-memory store.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code