LRU Cache
Updated
Problem statement
Implement a least recently used cache with fixed positive capacity, then process the operations in operations.
"PUT key value"inserts or updates a key. When a new key would exceed capacity, evict the least recently used key."GET key"returns the stored value, or-1when the key is absent.
A successful GET and every PUT make that key the most recently used. A missing GET does not change recency. Return the results of all GET operations in their original order.
Build the cache behavior directly; do not use a library-provided LRU cache.
Hardware follow-up: explain how to implement the same exact LRU policy in Verilog. Compare the C++ map/list organization with a hardware tag/value array, valid bits and recency counters. Explain hit detection, least-recent selection, recency updates and finite-counter overflow. Counters are one option; there is no single preferred circuit. The executable judge checks the cache behavior above and does not compile RTL or measure hardware cost. Preserve exact LRU; approximate pLRU is a separate policy.
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]GET 1 makes key 1 recent, so inserting key 3 evicts key 2.
Example 2
capacity = 1operations = ["PUT 4 7","PUT 4 9","GET 4","PUT 5 5","GET 4"]return = [9,-1]Updating key 4 changes its value to 9. Inserting key 5 later evicts it because capacity is 1.
Example 3
capacity = 2operations = ["GET 7","PUT 0 0","GET 0"]return = [-1,0]The first lookup misses without changing the cache. Keys and values may be zero.
Constraints
1 <= capacity <= 100000.1 <= operations.length <= 200000.- Every operation is exactly
"GET key"or"PUT key value". -10^9 <= key, value <= 10^9.
More AMD Onsite Interview questions
- Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
- Sort a Binary ArrayONSITE INTERVIEW · Seen Oct 2026
- Predict Runtime Method DispatchONSITE INTERVIEW · Seen Oct 2026
- K Largest Elements with QuickselectONSITE INTERVIEW · Seen Sep 2026
- Climbing StairsONSITE INTERVIEW · Seen Sep 2026
- First-Fit Arena AllocatorPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Coins for an AmountONSITE INTERVIEW · Seen Sep 2026
- Growing Circular QueueONSITE INTERVIEW · Seen Feb 2026
- Parse a Binary Firmware HeaderONSITE INTERVIEW · Seen Feb 2026
- Matrix Multiplication and Parallel PartitionsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 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
- 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
- Aligned Free-List Memory AllocatorHudson River Trading · ONSITE INTERVIEWHudson River Trading interview questions