← Questions

Key-value cache with per-entry TTL

0
NetflixCodingLast asked Oct 2026

Implement an in-memory key-value cache where every write gives a key, a value and a time-to-live. Once an entry's time is up it must behave as if it was never written. Support set, get and delete, and explain your choices as you go.

Follow-ups that come up:

  1. get is the hot path, so keep it cheap and move cleanup of expired entries elsewhere (a background sweeper or sidecar). How do you find expired entries without scanning everything?
  2. Add a capacity limit with least-recently-used eviction on top of the expiry.
  3. Make it safe for concurrent callers.
  4. Write and run tests, including ones that control the clock.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding