Implement a Key-Value Cache Where Every Entry Expires After the Same Fixed TTL
Company: Netflix
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Technical Screen
Implement an in-memory key-value cache in which every value expires after the same fixed duration, the time-to-live (TTL), which is set once when the cache is created. Once a value's TTL has passed, the cache must behave as if the key were absent. Then discuss the design: its complexity, possible optimizations, eviction, and how expired entries are cleaned up.
The report gives no interface. Assume the following and confirm it with the interviewer:
```python
class TTLCache:
def __init__(self, ttl: float): ...
def put(self, key, value) -> None: ...
def get(self, key): # the value, or None if the key is missing or expired
...
```
### Clarifying Questions
- Does writing a key that already exists restart its TTL?
- Does reading a key extend its TTL, or does only a write set it?
- Is an entry still valid at exactly the instant its TTL ends?
- Is there a limit on the number of entries or on memory, and what should go first when it is reached?
- Must expired entries release memory promptly, or is it enough that they are never returned?
- Will several threads use the cache at once?
### Part 1 — Implementation
Implement `put` and `get` with the expiry semantics above, aiming for constant time per operation.
```hint What one shared TTL tells you
Ask what a single TTL for every entry tells you about which entry will expire next.
```
```hint Controlling time
Make it possible to test expiry without sleeping in the tests.
```
#### What This Part Should Cover
- Correct expiry semantics for `get` and `put`, including rewrites of an existing key and the boundary instant
- A data structure choice that exploits the fixed TTL, with constant-time operations
- A clock suited to measuring durations, and tests that control time
### Part 2 — Complexity, optimizations, eviction and cleanup
State the time and space complexity of each operation. How would you optimize the design, how should eviction work if the cache also has a size limit, and how do expired entries get cleaned up so memory does not grow without bound?
```hint Lazy or proactive
Compare removing an expired entry only when it is touched with removing entries proactively, and think about the worst case of each.
```
#### What This Part Should Cover
- Amortized and worst-case complexity, and the memory held by keys that are written but never read again
- Cleanup strategies and their trade-offs in latency, memory and precision
- Eviction under a size limit and how it interacts with expiry
- Concurrency and per-entry memory overhead
### What a Strong Answer Covers
- Working code with correct expiry, rewrite and boundary behavior, verified with a controllable clock
- A structure that uses the fixed TTL to keep every operation O(1) amortized
- A clear account of when expired entries are removed, and a bound on memory
- A reasoned eviction policy for a size-limited cache
- Production concerns: thread safety, latency spikes from bulk expiry, clock choice
### Follow-up Questions
- If each key could have its own TTL, what would change in your design, and what would it cost?
- How would you make the cache safe for many threads without one lock becoming a bottleneck?
- The system clock jumps backward by an hour. What happens to your cache?
- Millions of keys were written in one burst and expire at the same moment. How do you avoid a long pause during cleanup?
Overview: Implement an in-memory key-value cache in which every entry expires after the same fixed time-to-live, then discuss its complexity, optimizations, eviction under a size limit, and how expired entries are cleaned up. It tests data structure choice, amortized analysis, testable time handling and concurrency awareness.