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.
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 Apple Phone Screen questions
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
- Maintain a Consistent-Hashing RingPHONE SCREEN · Seen Oct 2026
- Count Nodes in a ChainPHONE SCREEN · Seen Oct 2026
- Run-Length DecodingPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- LRU Key-Value Cache OperationsCohesity · PHONE SCREEN
- 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 CacheCoupang · PHONE SCREEN
- LRU Cache with Hit and Miss CountersLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Least Recently Used CacheGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- LRU CacheBlinkit · PHONE SCREENBlinkit interview questions
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)