Cached Lazy Token Buckets
Updated
Problem statement
Process operations in nondecreasing timestamp order.
["CONFIG", user, capacity, refillPerSecond, initialTokens, timestamp]creates that user's cached bucket.["ALLOW", user, cost, timestamp]lazily refills only that user's bucket, then tries to consumecosttokens.
At an ALLOW timestamp, add (timestamp - lastRefillTimestamp) * refillPerSecond tokens, capped at capacity, and advance the bucket's last-refill timestamp. If at least cost tokens are available, subtract them and return true; otherwise leave the refilled balance unchanged and return false.
Return one lowercase result string for each ALLOW operation.
Function
processTokenBuckets(operations: String[][]) → String[]Examples
Example 1
operations = [["CONFIG","fast","10","3","4","0"],["ALLOW","fast","5","0"],["ALLOW","fast","4","1"],["ALLOW","fast","8","2"],["ALLOW","fast","2","4"]]return = ["false","true","false","true"]The first request fails with only 4 tokens. At time 1 the bucket refills to 7 and spends 4; at time 2 it refills to 6, so cost 8 still fails. By time 4 it reaches capacity 10 and cost 2 succeeds.
Example 2
operations = [["CONFIG","fast","10","5","10","0"],["CONFIG","slow","4","1","1","0"],["ALLOW","slow","2","0"],["ALLOW","fast","8","0"],["ALLOW","slow","2","1"],["ALLOW","fast","5","1"]]return = ["false","true","true","true"]The two users have independent capacities, refill rates, balances, and timestamps. A denied request does not consume the slow user's single token.
Constraints
1 <= operations.length <= 200000.- Each user is configured exactly once before its first
ALLOW. 1 <= capacity, cost <= 10^18.0 <= refillPerSecond, initialTokens <= capacity.0 <= timestamp <= 10^18, and timestamps are globally nondecreasing.- User names are non-empty ASCII strings.
More xAI interview questions
- Live Top-K Spaces by Active UsersONSITE INTERVIEW · Seen Jan 2026
- Total Active Time by SpaceONSITE INTERVIEW · Seen Jan 2026
- Dynamic Batch Decode TraceONSITE INTERVIEW · Seen Dec 2025
- Nested Transaction Key-Value StoreONSITE INTERVIEW · Seen Oct 2025
- Weighted LRU CacheONSITE INTERVIEW · Seen Oct 2025
- LFU CacheONSITE INTERVIEW · Seen Sep 2025
- Least Recently Used CacheONSITE INTERVIEW · Seen Jun 2025
- Resumable List IteratorPHONE SCREEN · Seen Apr 2026
- Earliest Dataset Readiness in a PipelinePHONE SCREEN · Seen Mar 2026
- Largest Plus SignOA · Seen May 2025
Similar questions at other companies
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Hash Map with Separate ChainingApplied Intuition · ONSITE INTERVIEWApplied Intuition interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions