LRU Cache Operations
Updated
Problem statement
Process a sequence of operations on an initially empty least recently used (LRU) cache with a positive capacity.
get(key)returns the stored value or-1if absent. A successful get makes that key the most recently used; a miss does not change recency.put(key, value)inserts or updates the key and makes it most recently used. If an insertion exceeds capacity, evict the least recently used key.
For this exercise, operations are supplied as parallel arrays: operations[i] is get or put, and arguments[i] contains its one or two integer arguments. Return only get results, in operation order. The batch may be empty.
Function
runLruCache(capacity: int, operations: String[], arguments: int[][]) → int[]Examples
Example 1
capacity = 2operations = ["put","put","get","put","get","get"]arguments = [[1,10],[2,20],[1],[3,30],[2],[3]]return = [10,-1,30]Getting key 1 makes key 2 least recent. Inserting key 3 evicts key 2. The three gets return 10, -1 and 30.
Example 2
capacity = 1operations = ["put","put","get","put","get","get"]arguments = [[5,7],[5,0],[5],[6,8],[5],[6]]return = [0,-1,8]Updating key 5 retains one cache entry and stores zero. Inserting key 6 later evicts key 5.
Constraints
- For this exercise, assume
1 <= capacity <= 10^4and0 <= operations.length <= 10^4. - The two operation arrays have equal length and every operation has valid arguments.
- Keys and values are integers from 0 through
10^9.
More PeopleStrong Onsite Interview questions
- Add Two Binary StringsONSITE INTERVIEW · Seen Mar 2026
- Four SumONSITE INTERVIEW · Seen Mar 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Mar 2026
- Find the Entry of a Linked List CycleONSITE INTERVIEW · Seen Sep 2025
- Integer to English Words: Up to 20 DigitsONSITE INTERVIEW · Seen Sep 2025
- Word Ladder II: All Shortest TransformationsONSITE INTERVIEW · Seen Sep 2025
- Detect a Cycle in a Linked ListONSITE INTERVIEW · Seen May 2025
- First Nonrepeating CharacterONSITE INTERVIEW · Seen May 2025
- Longest Subarray with a Target SumONSITE INTERVIEW · Seen May 2025
- Lowest Common Ancestor in a BSTONSITE INTERVIEW · Seen May 2025
Similar questions at other companies
- LRU CacheLotusFlare · ONSITE INTERVIEW
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · 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
- First-Fit Memory AllocatorGoogle · ONSITE INTERVIEWGoogle interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- LRU CacheAMD · ONSITE INTERVIEWAMD interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions