{"id":"6b093ddfbd4755deb67ee0a4","slug":"token-bucket-rate-limiter","title":"Rate limiter – token bucket","summary":"Implement the token bucket algorithm – the same one API gateways and reverse proxies use. Lazy refilling, fractional tokens and an injected clock.","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"medium","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["design","rate-limiting","token-bucket","dependency-injection","time"],"languages":[{"lang":"typescript","runner":"browser","starter":"class TokenBucket {\n  /**\n   * @param capacity maximum number of tokens (the bucket starts full)\n   * @param refillPerSecond tokens added per second\n   * @param clock current time in milliseconds\n   */\n  constructor(capacity: number, refillPerSecond: number, clock: () => number = () => Date.now()) {\n    // TODO\n  }\n\n  /** Refills, then takes `tokens` if available. Never takes a part of them. */\n  tryConsume(tokens = 1): boolean {\n    throw new Error('Not implemented');\n  }\n\n  /** Refills and returns the current (possibly fractional) number of tokens. */\n  available(): number {\n    throw new Error('Not implemented');\n  }\n}\n","testCount":10,"tests":"function fakeClock(start = 0) {\n  const clock = () => clock.now;\n  clock.now = start;\n  return clock;\n}\n\ntest('starts full and rejects when empty', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(3, 1, clock);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(false);\n  expect(bucket.available()).toBe(0);\n});\n\ntest('refills over time', () => {\n  const clock = fakeClock(1000);\n  const bucket = new TokenBucket(2, 2, clock);\n  bucket.tryConsume(2);\n  clock.now += 500;\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(false);\n});\n\ntest('never exceeds the capacity', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(3, 1, clock);\n  bucket.tryConsume(3);\n  clock.now += 60000;\n  expect(bucket.available()).toBe(3);\n});\n\ntest('fractional tokens accumulate', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(2, 2, clock);\n  bucket.tryConsume(2);\n  clock.now = 250;\n  expect(bucket.available()).toBe(0.5);\n  expect(bucket.tryConsume()).toBe(false);\n  clock.now = 500;\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.available()).toBe(0);\n});\n\ntest('consumes several tokens at once, never partially', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(10, 5, clock);\n  expect(bucket.tryConsume(4)).toBe(true);\n  expect(bucket.tryConsume(7)).toBe(false);\n  expect(bucket.available()).toBe(6);\n  expect(bucket.tryConsume(6)).toBe(true);\n  expect(bucket.available()).toBe(0);\n});\n\ntest('a request larger than the capacity never succeeds', () => {\n  const bucket = new TokenBucket(5, 1, fakeClock());\n  expect(bucket.tryConsume(6)).toBe(false);\n  expect(bucket.available()).toBe(5);\n});\n\ntest('rejected calls still advance the refill time', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(1, 1, clock);\n  expect(bucket.tryConsume()).toBe(true);\n  clock.now = 500;\n  expect(bucket.tryConsume()).toBe(false);\n  clock.now = 1000;\n  expect(bucket.tryConsume()).toBe(true);\n  clock.now = 1500;\n  expect(bucket.available()).toBe(0.5);\n});\n\ntest('a clock going back adds nothing and is not counted twice', () => {\n  const clock = fakeClock(1000);\n  const bucket = new TokenBucket(4, 1, clock);\n  bucket.tryConsume(4);\n  clock.now = 500;\n  expect(bucket.available()).toBe(0);\n  clock.now = 1000;\n  expect(bucket.available()).toBe(0);\n  clock.now = 2000;\n  expect(bucket.available()).toBe(1);\n});\n\ntest('uses Date.now by default', () => {\n  const bucket = new TokenBucket(2, 0.001);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(false);\n});\n\ntest('performance: 100 000 calls, one per millisecond', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(10, 125, clock);\n  let accepted = 0;\n  for (let t = 0; t < 100000; t++) {\n    clock.now = t;\n    if (bucket.tryConsume()) accepted++;\n  }\n  expect(accepted).toBe(12509);\n  expect(bucket.available()).toBe(0.875);\n});\n"},{"lang":"javascript","runner":"browser","starter":"class TokenBucket {\n  /**\n   * @param {number} capacity maximum number of tokens (the bucket starts full)\n   * @param {number} refillPerSecond tokens added per second\n   * @param {() => number} clock current time in milliseconds\n   */\n  constructor(capacity, refillPerSecond, clock = () => Date.now()) {\n    // TODO\n  }\n\n  /** Refills, then takes `tokens` if available. Never takes a part of them. */\n  tryConsume(tokens = 1) {\n    throw new Error('Not implemented');\n  }\n\n  /** Refills and returns the current (possibly fractional) number of tokens. */\n  available() {\n    throw new Error('Not implemented');\n  }\n}\n","testCount":10,"tests":"function fakeClock(start = 0) {\n  const clock = () => clock.now;\n  clock.now = start;\n  return clock;\n}\n\ntest('starts full and rejects when empty', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(3, 1, clock);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(false);\n  expect(bucket.available()).toBe(0);\n});\n\ntest('refills over time', () => {\n  const clock = fakeClock(1000);\n  const bucket = new TokenBucket(2, 2, clock);\n  bucket.tryConsume(2);\n  clock.now += 500;\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(false);\n});\n\ntest('never exceeds the capacity', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(3, 1, clock);\n  bucket.tryConsume(3);\n  clock.now += 60000;\n  expect(bucket.available()).toBe(3);\n});\n\ntest('fractional tokens accumulate', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(2, 2, clock);\n  bucket.tryConsume(2);\n  clock.now = 250;\n  expect(bucket.available()).toBe(0.5);\n  expect(bucket.tryConsume()).toBe(false);\n  clock.now = 500;\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.available()).toBe(0);\n});\n\ntest('consumes several tokens at once, never partially', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(10, 5, clock);\n  expect(bucket.tryConsume(4)).toBe(true);\n  expect(bucket.tryConsume(7)).toBe(false);\n  expect(bucket.available()).toBe(6);\n  expect(bucket.tryConsume(6)).toBe(true);\n  expect(bucket.available()).toBe(0);\n});\n\ntest('a request larger than the capacity never succeeds', () => {\n  const bucket = new TokenBucket(5, 1, fakeClock());\n  expect(bucket.tryConsume(6)).toBe(false);\n  expect(bucket.available()).toBe(5);\n});\n\ntest('rejected calls still advance the refill time', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(1, 1, clock);\n  expect(bucket.tryConsume()).toBe(true);\n  clock.now = 500;\n  expect(bucket.tryConsume()).toBe(false);\n  clock.now = 1000;\n  expect(bucket.tryConsume()).toBe(true);\n  clock.now = 1500;\n  expect(bucket.available()).toBe(0.5);\n});\n\ntest('a clock going back adds nothing and is not counted twice', () => {\n  const clock = fakeClock(1000);\n  const bucket = new TokenBucket(4, 1, clock);\n  bucket.tryConsume(4);\n  clock.now = 500;\n  expect(bucket.available()).toBe(0);\n  clock.now = 1000;\n  expect(bucket.available()).toBe(0);\n  clock.now = 2000;\n  expect(bucket.available()).toBe(1);\n});\n\ntest('uses Date.now by default', () => {\n  const bucket = new TokenBucket(2, 0.001);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(true);\n  expect(bucket.tryConsume()).toBe(false);\n});\n\ntest('performance: 100 000 calls, one per millisecond', () => {\n  const clock = fakeClock();\n  const bucket = new TokenBucket(10, 125, clock);\n  let accepted = 0;\n  for (let t = 0; t < 100000; t++) {\n    clock.now = t;\n    if (bucket.tryConsume()) accepted++;\n  }\n  expect(accepted).toBe(12509);\n  expect(bucket.available()).toBe(0.875);\n});\n"},{"lang":"python","runner":"browser","starter":"import time\n\n\nclass TokenBucket:\n    def __init__(self, capacity: int, refill_per_second: float, clock=lambda: time.monotonic() * 1000) -> None:\n        \"\"\"clock() returns the current time in milliseconds; the bucket starts full.\"\"\"\n        pass  # TODO\n\n    def try_consume(self, tokens: int = 1) -> bool:\n        \"\"\"Refills, then takes `tokens` if available. Never takes a part of them.\"\"\"\n        raise NotImplementedError\n\n    def available(self) -> float:\n        \"\"\"Refills and returns the current (possibly fractional) number of tokens.\"\"\"\n        raise NotImplementedError\n","testCount":10,"tests":"class FakeClock:\n    def __init__(self, now=0):\n        self.now = now\n\n    def __call__(self):\n        return self.now\n\n\ndef eq(got, expected, what):\n    assert got == expected, f\"{what}: expected {expected}, got {got}\"\n\n\ndef test_starts_full_and_rejects_when_empty():\n    bucket = TokenBucket(3, 1, FakeClock())\n    for i in range(3):\n        eq(bucket.try_consume(), True, f\"call {i + 1}\")\n    eq(bucket.try_consume(), False, \"call 4\")\n    eq(bucket.available(), 0, \"available()\")\n\n\ndef test_refills_over_time():\n    clock = FakeClock(1000)\n    bucket = TokenBucket(2, 2, clock)\n    bucket.try_consume(2)\n    clock.now += 500\n    eq(bucket.try_consume(), True, \"after 500 ms\")\n    eq(bucket.try_consume(), False, \"second call after 500 ms\")\n\n\ndef test_never_exceeds_the_capacity():\n    clock = FakeClock()\n    bucket = TokenBucket(3, 1, clock)\n    bucket.try_consume(3)\n    clock.now += 60_000\n    eq(bucket.available(), 3, \"available() after a minute\")\n\n\ndef test_fractional_tokens_accumulate():\n    clock = FakeClock()\n    bucket = TokenBucket(2, 2, clock)\n    bucket.try_consume(2)\n    clock.now = 250\n    eq(bucket.available(), 0.5, \"available() at 250 ms\")\n    eq(bucket.try_consume(), False, \"try_consume() at 250 ms\")\n    clock.now = 500\n    eq(bucket.try_consume(), True, \"try_consume() at 500 ms\")\n    eq(bucket.available(), 0, \"available() at 500 ms\")\n\n\ndef test_consumes_several_tokens_never_partially():\n    bucket = TokenBucket(10, 5, FakeClock())\n    eq(bucket.try_consume(4), True, \"try_consume(4)\")\n    eq(bucket.try_consume(7), False, \"try_consume(7)\")\n    eq(bucket.available(), 6, \"available()\")\n    eq(bucket.try_consume(6), True, \"try_consume(6)\")\n    eq(bucket.available(), 0, \"available() at the end\")\n\n\ndef test_request_larger_than_capacity_never_succeeds():\n    bucket = TokenBucket(5, 1, FakeClock())\n    eq(bucket.try_consume(6), False, \"try_consume(6)\")\n    eq(bucket.available(), 5, \"available()\")\n\n\ndef test_rejected_calls_still_advance_the_refill_time():\n    clock = FakeClock()\n    bucket = TokenBucket(1, 1, clock)\n    eq(bucket.try_consume(), True, \"at 0 ms\")\n    clock.now = 500\n    eq(bucket.try_consume(), False, \"at 500 ms\")\n    clock.now = 1000\n    eq(bucket.try_consume(), True, \"at 1000 ms\")\n    clock.now = 1500\n    eq(bucket.available(), 0.5, \"available() at 1500 ms\")\n\n\ndef test_clock_going_back_adds_nothing():\n    clock = FakeClock(1000)\n    bucket = TokenBucket(4, 1, clock)\n    bucket.try_consume(4)\n    clock.now = 500\n    eq(bucket.available(), 0, \"after the clock went back\")\n    clock.now = 1000\n    eq(bucket.available(), 0, \"back at 1000 ms\")\n    clock.now = 2000\n    eq(bucket.available(), 1, \"at 2000 ms\")\n\n\ndef test_default_clock():\n    bucket = TokenBucket(2, 0.001)\n    eq(bucket.try_consume(), True, \"first call\")\n    eq(bucket.try_consume(), True, \"second call\")\n    eq(bucket.try_consume(), False, \"third call\")\n\n\ndef test_performance_100k_calls():\n    clock = FakeClock()\n    bucket = TokenBucket(10, 125, clock)\n    accepted = 0\n    for t in range(100_000):\n        clock.now = t\n        if bucket.try_consume():\n            accepted += 1\n    eq(accepted, 12509, \"accepted calls\")\n    eq(bucket.available(), 0.875, \"available() at the end\")\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic class TokenBucket\n{\n    /// <param name=\"clockMs\">current time in milliseconds; the bucket starts full</param>\n    public TokenBucket(int capacity, double refillPerSecond, Func<long> clockMs)\n    {\n        // TODO\n    }\n\n    /// <summary>Refills, then takes the tokens if available. Never takes a part of them.</summary>\n    public bool TryConsume(int tokens = 1)\n    {\n        throw new NotImplementedException();\n    }\n\n    /// <summary>Refills and returns the current (possibly fractional) number of tokens.</summary>\n    public double Available()\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":9},{"lang":"java","runner":"judge0","starter":"import java.util.function.LongSupplier;\n\nclass TokenBucket {\n    /** clockMs returns the current time in milliseconds; the bucket starts full. */\n    TokenBucket(int capacity, double refillPerSecond, LongSupplier clockMs) {\n        // TODO\n    }\n\n    boolean tryConsume() {\n        return tryConsume(1);\n    }\n\n    /** Refills, then takes the tokens if available. Never takes a part of them. */\n    boolean tryConsume(int tokens) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n\n    /** Refills and returns the current (possibly fractional) number of tokens. */\n    double available() {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":9},{"lang":"go","runner":"judge0","starter":"package main\n\ntype TokenBucket struct {\n\t// TODO\n}\n\n// NewTokenBucket creates a full bucket; clockMs returns the current time in milliseconds.\nfunc NewTokenBucket(capacity int, refillPerSecond float64, clockMs func() int64) *TokenBucket {\n\treturn &TokenBucket{}\n}\n\n// TryConsume refills, then takes the tokens if available. It never takes a part of them.\nfunc (b *TokenBucket) TryConsume(tokens int) bool {\n\tpanic(\"not implemented\")\n}\n\n// Available refills and returns the current (possibly fractional) number of tokens.\nfunc (b *TokenBucket) Available() float64 {\n\tpanic(\"not implemented\")\n}\n","testCount":9}],"questionCount":null,"xp":20,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:55.881Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A public API lets a client make short bursts but limits the average request rate. The most popular algorithm is the "},{"type":"text","text":"token bucket","marks":[{"type":"bold"}]},{"type":"text","text":": the bucket holds "},{"type":"text","text":"capacity","marks":[{"type":"code"}]},{"type":"text","text":" tokens, refills at a constant rate "},{"type":"text","text":"refillPerSecond","marks":[{"type":"code"}]},{"type":"text","text":", and every request takes tokens. When there are not enough tokens, the request is rejected (HTTP 429)."}]},{"type":"paragraph","content":[{"type":"text","text":"The clock is "},{"type":"text","text":"injected","marks":[{"type":"bold"}]},{"type":"text","text":" (a function returning the current time in milliseconds), so tests control time and never wait."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"paragraph","content":[{"type":"text","text":"A class "},{"type":"text","text":"TokenBucket(capacity, refillPerSecond, clock)","marks":[{"type":"code"}]},{"type":"text","text":":"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"the bucket starts "},{"type":"text","text":"full","marks":[{"type":"bold"}]},{"type":"text","text":","}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"tryConsume(tokens = 1)","marks":[{"type":"code"}]},{"type":"text","text":" – first refills for the time elapsed since the previous refill ("},{"type":"text","text":"elapsedMs · refillPerSecond / 1000","marks":[{"type":"code"}]},{"type":"text","text":", never above "},{"type":"text","text":"capacity","marks":[{"type":"code"}]},{"type":"text","text":"), then: if there are at least "},{"type":"text","text":"tokens","marks":[{"type":"code"}]},{"type":"text","text":" tokens – subtracts them and returns "},{"type":"text","text":"true","marks":[{"type":"code"}]},{"type":"text","text":", otherwise returns "},{"type":"text","text":"false","marks":[{"type":"code"}]},{"type":"text","text":" and "},{"type":"text","text":"subtracts nothing","marks":[{"type":"bold"}]},{"type":"text","text":","}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"available()","marks":[{"type":"code"}]},{"type":"text","text":" – refills and returns the current (possibly fractional) number of tokens,"}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"a request larger than "},{"type":"text","text":"capacity","marks":[{"type":"code"}]},{"type":"text","text":" never succeeds,"}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"if the clock goes back in time, nothing is added and the reference point stays at the latest time seen so far (so the same period is not counted twice when the clock catches up)."}]}]}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Language"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"API"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"JS/TS"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"new TokenBucket(capacity, refillPerSecond, clock = () => Date.now())","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"tryConsume(tokens = 1)","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"available()","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Python"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"TokenBucket(capacity, refill_per_second, clock=<monotonic clock in ms>)","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"try_consume(tokens=1)","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"available()","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"C#"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"new TokenBucket(int capacity, double refillPerSecond, Func<long> clockMs)","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"TryConsume(int tokens = 1)","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"Available()","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Java"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"new TokenBucket(int capacity, double refillPerSecond, LongSupplier clockMs)","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"tryConsume()","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"tryConsume(int tokens)","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"available()","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Go"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"NewTokenBucket(capacity int, refillPerSecond float64, clockMs func() int64)","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"TryConsume(tokens int) bool","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"Available() float64","marks":[{"type":"code"}]}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"example"},"content":[{"type":"text","text":"Example"}]},{"type":"paragraph","content":[{"type":"text","text":"capacity = 2","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"refillPerSecond = 2","marks":[{"type":"code"}]},{"type":"text","text":" (one token every 500 ms):"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Time [ms]"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Call"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Result"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Tokens after"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"tryConsume()","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"true","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"1"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"tryConsume()","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"true","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"tryConsume()","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"false","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"250"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"available()","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0.5","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0.5"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"500"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"tryConsume()","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"true","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"10,000"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"available()","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"2","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"2 (never above the capacity)"}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"constraints"},"content":[{"type":"text","text":"Constraints"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"1 ≤ capacity ≤ 10⁶","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"0 < refillPerSecond ≤ 10⁶","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"1 ≤ tokens","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Up to 100,000 calls per test."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"expected-complexity"},"content":[{"type":"text","text":"Expected complexity"}]},{"type":"paragraph","content":[{"type":"text","text":"Every operation "},{"type":"text","text":"O(1)","marks":[{"type":"bold"}]},{"type":"text","text":" time and memory – no queues of timestamps."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:55.882Z","updatedAt":"2026-11-27T08:22:55.882Z"}