LRU Cache with TTL
Updated
Problem statement
Design a cache, kept in memory, with space for up to capacity keys. Every key maps to an integer value and carries a time to live, or TTL. When a key is written with TTL ttl at time t, it expires at t + ttl. From that moment it is gone from the cache: it no longer takes up room, and looking it up finds nothing.
The cache has two calls, and each call carries the current time:
put(key, value, ttl, time)storesvalueunderkey, and the key's TTL starts attime. Ifkeyis present, its value is overwritten and its TTL restarts. Ifkeyis absent and allcapacityslots are taken, first drop the least recently used key to make room.get(key, time)looks upkey: it returns the stored value whenkeyis present and-1otherwise.
A key is used when a put stores it or a get finds it. A get that returns -1 uses no key. Before each call runs, every key whose expiry time is at most the call's time has already expired.
You receive the calls as two equal-length lists, in call order. operations[i] names the call, "put" or "get", and params[i] holds its numbers: [key, value, ttl, time] for a put and [key, time] for a get. Process every call and return what each get returned, in call order.
Follow-up: what are the limitations of this cache?
Function
runCache(capacity: int, operations: String[], params: int[][]) → int[]Examples
Example 1
capacity = 2operations = ["put","put","get","put","get","get","get"]params = [[7,70,3,1],[8,80,10,2],[7,3],[9,90,10,5],[8,6],[7,6],[9,6]]return = [70,80,-1,90]Key 7 is written at time 1 with TTL 3, giving an expiry of 4. At time 3 a get still finds key 7, so key 7 is now more recently used than key 8. Key 7 is gone by time 5, leaving only key 8, so key 9 fits with nothing evicted. At time 6 the three gets return 80, -1 for the expired key 7, and 90.
Example 2
capacity = 2operations = ["put","put","get","put","get","put","get","get"]params = [[1,100,50,0],[2,200,50,0],[1,10],[3,300,50,20],[2,21],[1,111,5,22],[3,26],[1,27]]return = [100,-1,300,-1]The get at time 10 uses key 1, so key 2 becomes the least recently used. At time 20 both slots are taken and key 3 is new, so key 2 is dropped. Writing key 1 again at time 22 sets its value to 111 and pushes its expiry to 22 + 5 = 27; so the final get, at exactly time 27, finds key 1 gone and returns -1.
Example 3
capacity = 1operations = ["get","put","get","put","get","get"]params = [[5,0],[5,1,1,0],[5,0],[6,2,100,0],[5,0],[6,99]]return = [-1,1,-1,2]Several calls may carry equal times. The opening get returns -1 because nothing is stored yet, and the next get returns 1. Capacity is 1, so writing key 6 evicts key 5. Key 6 lasts until time 100, so a get at time 99 still returns 2.
Constraints
1 <= capacity <= 10001 <= operations.length <= 104params.length == operations.length- Every
operations[i]equals"put"or"get"; at least one equals"get". params[i]holds 4 numbers[key, value, ttl, time]whenoperations[i]is"put"and 2 numbers[key, time]when it is"get".0 <= key, value, time <= 109and1 <= ttl <= 109- The
timevalues never decrease from one call to the next.
More Patreon interview questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
- Constant-Time Key Store Built on ArraysONSITE INTERVIEW · Seen Dec 2021
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
- Three SumOA · Seen Apr 2025
Similar questions at other companies
- Balanced Session ManagerPalantir · PHONE SCREENPalantir interview questions
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Thread-Safe LRU Cache with TTLLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Fixed Window Average and ModeNuro · PHONE SCREENNuro interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheAthenahealth · PHONE SCREEN
- Paged KV Cache AccessNVIDIA · PHONE SCREENNVIDIA interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- LRU CacheLotusFlare · ONSITE INTERVIEW