Fixed-Capacity Cache with Least-Recently-Used Eviction

Asked at Meta · Coding Questions · Difficulty: Medium · Last asked Jan 13, 2026

Build a key-value cache that holds at most `capacity` entries. When a new key arrives and the cache is already full, the cache throws out the entry that has gone the longest without being read or written (the least recently used one). Both reading and writing must take constant time on average, however large the cache is.

Loading the problem…