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.
Interview follow-up
The interviewer also asked for a generic implementation. Discuss an LRU cache that works for any key and value types. Cover how keys are compared and hashed, what must always be true about the map and the linked list, and how to report a missing key with a proper result type instead of returning the number -1. Your function here still uses integer keys and values.
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 Navan interview questions
- Roman to IntegerPHONE SCREEN · Seen Oct 2024
- Sort ColorsPHONE SCREEN · Seen Oct 2024
- Basic CalculatorPHONE SCREEN · Seen Jun 2024
- House RobberPHONE SCREEN · Seen May 2024
- House Robber IIPHONE SCREEN · Seen May 2024
- Snake-Case Iterator to Camel CasePHONE SCREEN · Seen Feb 2024
- Lexicographically Largest Distinct-Letter SubsequencePHONE SCREEN · Seen May 2023
- Time Based Key-Value StoreONSITE INTERVIEW · Seen Dec 2025
- Type-Ahead Prefix MatchesONSITE INTERVIEW · Seen Dec 2024
- Minimum Steps on a Circular Character DialONSITE INTERVIEW · Seen Jan 2024
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 CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- LRU CacheCoupang · PHONE SCREEN
- LRU CacheBlinkit · PHONE SCREENBlinkit interview questions
- LRU Cache with Thread-Safety Follow-UpSnap Inc. · PHONE SCREENSnap Inc. interview questions
- Least Recently Used CacheIllumio · PHONE SCREENIllumio interview questions