Decide Which API Requests a Token Bucket Rate Limiter Admits
Last updated: November 29, 2026
Quick Overview
Simulate a token bucket with a given capacity that starts full and refills at r tokens per second, up to capacity. Given sorted request timestamps in seconds, each costing one token, return a list of booleans saying whether each request is admitted.
0
0
0 solved
Simulate a token bucket with a given capacity that starts full and refills at r tokens per second, up to capacity. Given sorted request timestamps in seconds, each costing one token, return a list of booleans saying whether each request is admitted.
Google Cloud APIs enforce quotas, and this screen question checks whether you can turn a familiar policy into a precise simulation. The interviewer looks for lazy refill computed from elapsed time rather than a per-second loop, and careful floating point handling.
What the Interviewer Expects
- Refill lazily using elapsed time since the last request
- Cap tokens at capacity after refill
- Admit only if at least one full token is available
- Avoid simulating every second between requests
- Discuss floating point versus integer milli-token arithmetic
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support requests with different costs?
- How would you shard this limiter across many API front ends?
- How would you return the earliest time a rejected request could succeed?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs: capacity (int), rate (tokens per second, may be fractional), and a non-decreasing list times of request timestamps. The bucket is full at the first request time. Each request needs one t...
High-Level Design
State is (tokens, last_time). On each request, add (t - last_time) * rate tokens, cap at capacity, set last_time = t. If tokens >= 1, subtract one and admit, else reject. This is O(1) per requ...