LLD Guide · Module 2: Problem prep · Lesson 02.03
Prep: LRU Cache
Two structures, one recency order, O(1) everything, and one method that must not touch the order.
By Cruscible Engineering · Published
Every LRU cache is a map plus an order. The bugs come from the moments they disagree.
This lesson prepares you for Build an LRU Cache. It doesn't solve it.
1. What the LRU cache contract asks for
A cache with a fixed capacity. When it's full and a new key arrives, the least recently used entry goes. get and put count as use; containsKey doesn't.
| Method | Returns / does |
|---|---|
init(capacity) | sets the capacity (≥ 1); called once, first |
get(key) | the value, or null; marks the key as most recently used |
put(key, value) | inserts or updates; marks it most recently used; evicts the LRU entry first if full and the key is new |
remove(key) | true if the key existed |
size() | current entries, never above capacity |
containsKey(key) | true if present, without changing the order |
All of get, put and remove must be O(1) amortised, at 50,000 operations a second with P99 under 1 ms, with capacities up to 10,000.
Figure: The brief's own example, step by step: what get, put and containsKey do to the order
- init(3), then put a, b and c: the cache is full and c is the most recent.
- get(a) makes a the most recent, so b is now the oldest.
- put(d) evicts b, the least recently used.
- containsKey(c) doesn't change the order, so put(e) evicts c.
2. Lessons to read before the LRU cache
- 01.02 Thread-safety: "state spread over two structures" is the whole problem.
- 01.01 How LLD problems work: why O(n) scans lose performance points even when every scenario passes.
3. LRU cache design decisions to make first
- What gives you O(1) lookup, and what gives you O(1) "move to front" and "remove oldest"? Name both structures and what each holds.
- What happens on
putof a key that already exists? Does it evict anything? What happens to its position? - Which order of steps keeps the map and the order consistent? Especially in
putwith an eviction. - Where does the provided KV store fit? The brief says to use it for the underlying data. What do you keep in the KV store, and what in your own structure?
- What is your locking plan? Note that
getwrites: it changes the order. Does a read-write lock still help?
4. LRU cache pitfalls
⚠️ Trap:
containsKeymust not promote the key. If your structure reorders on every access (for example aLinkedHashMapin access order),containsKeymust avoid that path.
⚠️ Trap: updating an existing key never evicts, even when the cache is full.
⚠️ Trap:
getchanges the order, so it is a write as far as locking is concerned. Two concurrentgetcalls under a read lock can corrupt the order.
Before you submit
getreturnsnullfor a missing key and marks a present key as most recently usedputon a new key evicts the least recently used entry only when the cache is fullputon an existing key updates the value, moves it to most recent, and evicts nothingcontainsKeynever changes the orderremovereturnstrueonly if the key existed, and frees its placesizenever exceeds the capacityget,putandremoveare O(1): no scans of the whole cache- Concurrent calls never leave the map and the order disagreeing
Further reading
After you've tried it
Submit first. The full walkthrough of one solution: Building an LRU Cache with O(1) Operations.