Design an LRU cache 00:00:00 Make a prediction You will trace eviction by last access, not insertion order, and maintain agreement between key lookup and recency order. A two-entry cache receives A, B, reads A, then inserts C. Which key should disappear? B. Reading A moved it ahead of B in recency. Evicting A because it arrived first would be first-in, first-out, not least recently used. 00:00:28 Three coats, and a fourth one arrives A cloakroom hangs recently worn coats at the front and keeps a button pointing to each coat. A green coat moved to the front after use survives while the untouched blue coat at the back yields its hook. The buttons are the lookup table and the row of hooks is recency order. One operation must update both representations or a button can point at a missing coat. A cache keeps copies of things that are slow to get. It has little room, so it must choose what to keep. Least recently used, LRU, is one such choice: when the room is full, throw out the entry that nobody has touched for the longest time. The rule works because of temporal locality: what was used a moment ago is likely to be used again soon. This is a habit of programs and people, not a law. A report that reads every row once breaks it, and this design shows what to do then. 00:01:23 Evict the truly least recent item A two-entry cache receives A then B. Reading A makes it most recently used, even though it was inserted first. The lookup table finds A's node directly; the recency list moves it to the front. Now B is the least recent. Insert C and remove B from both structures. A read of B must miss. Evicting whichever key was inserted earliest implements a different policy. Write A and B, read A, then insert C. B leaves because the read refreshed A. Look up B again and confirm that it misses. If B's list node was removed but its map entry survives, a cache can report the right size while returning a value it already evicted. 00:02:14 Updates must refresh the right node An existing key receives a new value. Update its value and recency according to the contract without adding a duplicate list node. Test that the cache remains within capacity and evicting later removes exactly one mapping. Overwrite A three times in a capacity-two cache. There should still be one A node and one map entry, with the latest value. An implementation that appends a new node on each update may evict a live key while an older duplicate node still occupies its list. A pure lookup in an LRU cache often mutates recency. Treating it as a read-only operation when locking can corrupt the list. If approximate eviction is acceptable at high concurrency, explain the hit-rate and complexity tradeoff before relaxing exact ordering. Python offers a decorator that remembers recent results of a function. Java offers a linked hash map that can maintain entries in access order. These two public interfaces serve different callers, yet both illustrate how a record of recent use can guide eviction without scanning for a victim on every insertion. 00:03:25 Try an interview follow-up How would you make eviction pluggable while keeping each operation constant time? The policy must hear events and keep its own structure: added, used, removed, victim. An interface that receives all entries and picks one forces a search. I would also define who holds the lock when the policy is called, and whether it may say no to a newcomer, because admission is where modern policies win. A cache shrinks from three entries to one while the service is running. Which two keys should disappear? Evict repeatedly from the least-recent end until the size fits. Update both lookup and order on each removal. If a key was touched just before the resize, its refreshed position determines whether it survives. 00:04:18 What to take with you An LRU cache is a hash map and a doubly linked list over the same nodes. The map answers where, the list answers who is last. Every use moves a node to the front, and a full cache gives up the node at the back. Both calls are constant time. The invariant is one sentence: the map and the list hold the same nodes, at most as many as the capacity. One class changes both, so the sentence can be checked after each call.