Least Recently Used Cache
Updated
Problem statement
Process a sequence of operations on a least recently used cache with capacity capacity.
PUT key valueinserts or updates a key. A successful update makes that key the most recently used. If inserting a new key exceeds capacity, evict the least recently used key.GET keyreturns the stored value, or-1when the key is absent. A successful lookup makes that key the most recently used; a missing lookup does not change recency.
Return the results of the GET operations in their original order. Keys and values are decimal integers separated by one space in each operation string.
Function
runLRU(capacity: int, operations: String[]) → int[]Examples
Example 1
capacity = 2operations = ["PUT 1 10","PUT 2 20","GET 1","PUT 3 30","GET 2","GET 3"]return = [10,-1,30]The lookup of key 1 makes key 2 least recently used, so inserting key 3 evicts key 2.
Example 2
capacity = 1operations = ["PUT 5 7","PUT 5 9","GET 5","PUT 6 4","GET 5"]return = [9,-1]Updating key 5 replaces its value without adding a second entry. Inserting key 6 later evicts it.
Example 3
capacity = 3operations = ["GET 8","PUT 8 0","GET 8"]return = [-1,0]The first lookup misses. After insertion, a stored value of 0 is returned normally.
Constraints
1 <= capacity <= 10^51 <= operations.length <= 2 * 10^5- Every operation is exactly
GET keyorPUT key value. -10^9 <= key, value <= 10^9
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
- Cached Lazy Token BucketsONSITE INTERVIEW · Seen Sep 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
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- LRU CacheAMD · ONSITE INTERVIEWAMD interview questions
- Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- LRU Cache with Hit and Miss CountersLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Least Recently Used CacheGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- LRU CacheIntuit · ONSITE INTERVIEWIntuit interview questions
- LRU CacheOracle · ONSITE INTERVIEWOracle interview questions