Fixed-Size Least Recently Used Cache
Updated
Problem statement
Simulate a cache of integer keys and integer values. The cache begins with no entries and never holds more than capacity of them at once.
Each stored key remembers when it was last touched. Two things touch a key: a get that finds it, and any put on it, whether the key is new or only gets a new value. A get that misses touches nothing.
"get"with[key]: the result is the value stored underkey, or-1ifkeyhas no entry."put"with[key, value]: afterwardskeymaps tovalue. Whenkeyis new and every slot is taken, the entry touched longest ago is dropped first to make room. Giving an existing key a new value never drops anything.
Call number i is named by operations[i], and params[i] holds its numbers. Process the calls from first to last. Your function returns an array holding what each get produced, in call order; a put adds nothing to it, so the array can be empty.
Follow-up: design it so a single call does O(1) work on average.
Function
processCacheCalls(capacity: int, operations: String[], params: int[][]) → int[]Examples
Example 1
capacity = 3operations = ["put","get","put","put","get","put","get","get","put","get","get"]params = [[10,1],[10],[20,2],[30,3],[20],[40,4],[10],[30],[50,5],[20],[50]]return = [1,2,-1,3,-1,5]Room for 3 entries. The first get finds key 10 (value 1). The puts of 20 and 30 fill the cache, and reading 20 (value 2) leaves 10 as the key touched longest ago. So putting 40 drops 10, and the next get of 10 misses (-1). Reading 30 (value 3) leaves 20 as the stalest key; putting 50 drops it, so get 20 gives -1 and get 50 gives 5.
Example 2
capacity = 1operations = ["get","put","put","get","put","get","get"]params = [[3],[3,1],[3,8],[3],[4,2],[3],[4]]return = [-1,8,-1,2]Room for 1 entry. Reading 3 before anything is stored gives -1. The second put on key 3 only replaces its value, so reading it gives 8. Putting key 4 needs the only slot and pushes key 3 out: reading 3 gives -1 and reading 4 gives 2.
Example 3
capacity = 3operations = ["put","put","put","put","put","get","get","get","get"]params = [[1,10],[2,20],[3,30],[1,11],[4,40],[2],[1],[3],[4]]return = [-1,11,30,40]Room for 3 entries, filled by keys 1, 2 and 3. Putting 11 under key 1 touches key 1 but drops nothing, because that key was already stored. Key 2 is now the stalest, so putting key 4 drops it. The four reads give -1, 11, 30 and 40.
Constraints
1 <= capacity <= 8001 <= operations.length <= 2400params.length == operations.lengthoperations[i]is one of"get"and"put".params[i]has one number,[key], for a get and two numbers,[key, value], for a put.0 <= key <= 5 * 10^4and0 <= value <= 10^6
More OpenAI Onsite Interview questions
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Message Event AggregationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- 15-Minute Chat Session TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Segmented Durable Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Durable Work Queue OperationsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Versioned Social NetworkPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Budgeted API Inference with Out-of-Order FeedbackONSITE INTERVIEW · Seen Oct 2026
- Plant Infection Simulation, Part 4: Death CountdownPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Count Friend CirclesPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
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
- LRU CacheLotusFlare · 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
- 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