Weighted LRU Cache
Updated
Problem statement
Process operations against an initially empty cache with total weight capacity capacity.
["PUT", key, value, weight]inserts or replaces an item and makes it most recently used.["GET", key]returns the value and makes a present item most recently used; a miss returnsNULL.
After every PUT, while the sum of resident item weights exceeds capacity, evict the least recently used item. Replacing a key removes its previous weight before adding the new item. A single insertion may therefore evict multiple items.
Return the outputs of GET operations in order.
Function
weightedLru(capacity: int, operations: String[][]) → String[]Examples
Example 1
capacity = 5operations = [["PUT","a","A","3"],["PUT","b","B","2"],["GET","a"],["PUT","c","C","4"],["GET","a"],["GET","c"]]return = ["A","NULL","C"]The hit on a makes b least recent. Adding weight-4 c first evicts b, then still exceeds capacity and evicts a.
Example 2
capacity = 5operations = [["PUT","a","old","2"],["PUT","b","B","3"],["PUT","a","new","1"],["PUT","c","C","2"],["GET","b"],["GET","a"],["GET","c"]]return = ["NULL","new","C"]Replacing a changes its weight from 2 to 1 and refreshes its recency. Adding c then evicts b.
Constraints
1 <= capacity <= 10^9.1 <= operations.length <= 200000.- Each weight is an integer in
[1, capacity]. - Keys and values are non-empty ASCII strings; values are never
NULL. - The sum of all operation-string lengths is at most
2000000.
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
- LFU CacheONSITE INTERVIEW · Seen Sep 2025
- Cached Lazy Token BucketsONSITE 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
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- LRU CacheLotusFlare · ONSITE INTERVIEW
- LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- LRU Cache OperationsHevo Data · ONSITE INTERVIEW
- LRU Cache OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- LRU Cache Operation SequenceGoogle · ONSITE INTERVIEWGoogle interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- LRU CacheAMD · ONSITE INTERVIEWAMD interview questions