26A key-value cache with a time to live
26 A key-value cache with a time to live
Netflix candidates report a key-value store with TTL in a phone screen (1 report), a second coding round (1 report) and an onsite loop (1 report). For one Senior candidate the whole prompt was a one-liner: design a cache with TTL (1 source). In one report the clarifying question that mattered was whether all entries share one expiry policy or each gets its own, and each its own was the better answer (1 report). The follow-up there was what to do when memory is full, and a Netflix note on Hello Interview lists cleanup as a separate job, memory bounds such as LRU or FIFO, and spreading it over more nodes like Redis (1 source). One candidate built it on a min-heap keyed on expiry time, wrote tests, and was still turned down for code that wasn't concise and direct (1 report). LeetCode's Netflix tags list Cache With Time Limit too (2 sources). This is practice in that shape, not a leaked question or LeetCode's version.
Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/26_ttl_cache -k level1. Earlier levels must keep passing.
Build TTLCache in solution.py. All time comes from clock, a function that returns the current time in
seconds (time.monotonic by default). Never read the system clock yourself: the tests pass a fake clock and move it
by hand, which is how you'd test this at work too.
Level 1. TTLCache(default_ttl=None, clock=time.monotonic). put(key, value, ttl=None) stores a value. An
entry written at time t with TTL ttl is live while t <= now < t + ttl. ttl=None falls back to default_ttl,
and with neither the entry never expires. Writing a key again replaces its value and restarts its clock.
get(key, default=None) returns a live entry's value, else default. delete(key) removes the key and returns
True if it was live. A ttl or default_ttl that isn't positive raises ValueError and stores nothing.
Level 2. Cleanup. An expired entry still takes memory until something removes it. len(cache) is the number of
live entries. purge() removes every expired entry still stored and returns how many it removed. Make purge cost
what it removes, not the size of the cache: one test counts the time comparisons a purge makes. A min-heap of expiry times is the usual tool (1 report), and it will
still hold times for keys you've since written again or deleted.
Level 3. A memory bound. TTLCache(default_ttl=None, capacity=None, clock=time.monotonic) stores at most
capacity entries, expired ones still stored included. When put adds a new key to a full cache, it first removes the expired entries, and if the
cache is still full it evicts the least recently used one. A put and a get that finds a live entry both count as
a use. put returns the key it evicted, or None. Writing a key that's already stored never evicts anything. A put
whose ttl isn't positive raises before it evicts anything. A capacity below 1 raises ValueError.
Things to get right: the expiry boundary, heap entries that no longer match their key, and two orders over the same entries (expiry for cleanup, recency for eviction) that must agree on what's stored. Write a few tests of your own as you go. Two of these candidates did, and one included an exception case (2 reports). Follow-ups from the reports: running cleanup as a separate job, and spreading the cache over several nodes (1 source). Once a cleanup job runs next to your requests, be ready to talk about locking too.