LRU Cache Operations
Updated
Problem statement
Process a sequence of operations on an initially empty least recently used (LRU) cache with the given positive capacity. Each operation is a triple:
[0,key,0]gets the value stored forkey. Return-1for a missing key. A successful get makes that key the most recently used; a miss does not change the cache.[1,key,value]inserts a key or replaces its value. That key becomes the most recently used. If the cache exceeds its capacity, evict the least recently used key.
Return an array containing the result of every get, in operation order. A put produces no result. If there are no get operations, return an empty array.
Interview context
The interview supplied boilerplate and four sequential questions within an LRU implementation exercise. All implementation, debugging, and data-model edits had to be made through AI. This task covers the basic LRU operations described above.
Function
lruCache(capacity: int, operations: int[][]) → int[]Examples
Example 1
capacity = 2operations = [[1,1,10],[1,2,20],[0,1,0],[1,3,30],[0,2,0],[0,1,0],[0,3,0]]return = [10,-1,10,30]After the first two puts, key 1 is least recent. Getting key 1 makes key 2 least recent, so putting key 3 evicts key 2. The four gets return [10,-1,10,30].
Example 2
capacity = 1operations = [[1,4,9],[1,4,0],[0,4,0],[1,5,7],[0,4,0]]return = [0,-1]Updating key 4 stores 0. Its get returns 0. Inserting key 5 then evicts key 4, so the next get returns -1.
Constraints
1 <= capacity <= 10000.0 <= operations.length <= 100000.- Every operation contains exactly
3integers. - Each operation is
[0,key,0]for a get or[1,key,value]for a put. - Every operation entry is an integer from
0through1000000000. - For a get operation, the third integer is
0.
Source note: Source images are source-neutral excerpts of author-reported interview prompts.
More LinkedIn Onsite Interview questions
- Explore an Unknown Grid and Find a PathONSITE INTERVIEW · Seen Nov 2026
- Fire Station Response BillONSITE INTERVIEW · Seen Nov 2026
- Roll a Weighted DieONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Subarray With at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- Kth Closest Element in a BSTONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Window with at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- Lexicographically Smallest Course OrderONSITE INTERVIEW · Seen Oct 2026
- K Closest Values in a Binary Search TreeONSITE INTERVIEW · Seen Oct 2026
- Unique Single-Use Sum CombinationsONSITE INTERVIEW · Seen Oct 2026
- Maximum Consecutive Ones in a Circular ArrayONSITE INTERVIEW · Seen Oct 2026
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 Operation SequenceGoogle · 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
- Least Recently Used CacheGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions