FastPrepLRU Cache with TTL
Problems/LRU Cache with TTL

LRU Cache with TTL

Patreon logoPatreon● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported by six engineering candidates at Patreon in 2021 and 2022, two of them at senior level. Three reports name an LRU cache whose entries also expire after a time to live (TTL). In 2022, one placed it in a virtual three-person interview that came after a phone screen on another topic; another had a phone screen and then three rounds, and says a hint from the interviewer in the technical round led to a bug; the third, from a candidate who got an offer, does not say which round. The other three reports ask for an LRU cache without naming a TTL: a 2022 senior candidate solved it first in a one-hour live coding call, a 2021 senior candidate lists a question on its limitations next to it and does not say which round, and a 2021 candidate had it in a design interview, to be written in Java. Here the TTL is part of the task for everyone. We list the three-person interview, the design interview and the technical round as onsite rounds; for the technical round this is our reading, since that report does not tie its questions to a round. We list the one-hour live coding call as a phone screen, our reading of a single coding call with no later round, and also list the two reports that do not say which round as phone screens.

The reports do not say how the calls are made, how time is given, whether an expired entry still takes up room, or what a miss returns. Here, each call carries the current time, a key expires at the time it was stored plus its TTL, expired keys are gone before each call and free their room, a miss returns -1, keys and values are non-negative integers, and you get the calls as a list of operations instead of a class. All limits are ours. The limitations follow-up is a discussion question answered in the editorial, not tested.

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) stores value under key, and the key's TTL starts at time. If key is present, its value is overwritten and its TTL restarts. If key is absent and all capacity slots are taken, first drop the least recently used key to make room.
  • get(key, time) looks up key: it returns the stored value when key is present and -1 otherwise.

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 <= 1000
  • 1 <= operations.length <= 104
  • params.length == operations.length
  • Every operations[i] equals "put" or "get"; at least one equals "get".
  • params[i] holds 4 numbers [key, value, ttl, time] when operations[i] is "put" and 2 numbers [key, time] when it is "get".
  • 0 <= key, value, time <= 109 and 1 <= ttl <= 109
  • The time values 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
All Patreon interview questionsSee Patreon hiring insights
Saved
class Solution {
    public int[] runCache(int capacity, String[] operations, int[][] params) {
        // Write your code here.
    }
}
Checking account…