Build an LRU cache with a hash map and a doubly linked list, so get and put both take O(1).
The classic interview question: build an LRU cache where both get and put take O(1) time. The answer is two structures working together: a hash map to find an entry instantly, and a doubly linked list to keep entries in order of use.
The page "Cache Eviction Policies" explains what LRU means: when the cache is full, throw out the item that was used longest ago. This page is about how to build it so that it is fast, which is what an interview or a real library has to solve.
Think about what each operation needs. Find a key: a hash map does this in O(1). Mark it as just used: it must move to the "newest" end. Find the oldest: it must be at the "oldest" end. A hash map alone has no order. A list alone cannot find a key quickly. Together they cover every step.
Why a doubly linked list? To remove a node from the middle in O(1), you must fix the node before it and the node after it. Each node therefore needs a prev and a next pointer. With a singly linked list you would have to walk to find the previous node, and that is O(n).
| Operation | Steps | Cost |
|---|---|---|
| get(key) | Look up the node in the map. If missing, return not found. Otherwise move it to the front and return the value. | O(1) |
| put(key, value), key exists | Find the node, change its value, move it to the front. | O(1) |
| put(key, value), key is new, room left | Make a node, add it after HEAD, add it to the map. | O(1) |
| put(key, value), key is new, cache full | Remove the node before TAIL, delete its key from the map, then insert the new node. | O(1) |
| Idea | Find a key | Update recency | Evict oldest | Verdict |
|---|---|---|---|---|
| Hash map only | O(1) | No order kept | O(n) to find oldest | Cannot evict fast |
| Array or queue only | O(n) | O(n) to move | O(1) | Too slow to find and move |
| Singly linked list + map | O(1) | O(n) to find the previous node | O(n) for the tailโs previous | Needs the prev pointer |
| Map + doubly linked list | O(1) | O(1) | O(1) | The standard answer |
| Map with a timestamp per key | O(1) | O(1) | O(n) scan to find the smallest | Simple but slow to evict |
Try this: capacity 3, put A, B, C, then get A, then put D. B is evicted, not A, because reading A made it recent. Every step is a hash lookup plus a few pointer changes, so it stays O(1) however big the cache is.
class N { constructor(public k = 0, public v = 0, public prev?: N, public next?: N) {} }
class LRU {
private map = new Map<number, N>();
private head = new N(); private tail = new N();
constructor(private cap: number) { this.head.next = this.tail; this.tail.prev = this.head; }
private remove(n: N) { n.prev!.next = n.next; n.next!.prev = n.prev; }
private addFront(n: N) {
n.next = this.head.next; n.prev = this.head;
this.head.next!.prev = n; this.head.next = n;
}
get(k: number): number {
const n = this.map.get(k); if (!n) return -1;
this.remove(n); this.addFront(n); return n.v;
}
put(k: number, v: number) {
let n = this.map.get(k);
if (n) { n.v = v; this.remove(n); this.addFront(n); return; }
if (this.map.size === this.cap) {
const old = this.tail.prev!; this.remove(old); this.map.delete(old.k);
}
n = new N(k, v); this.map.set(k, n); this.addFront(n);
}
}Map remembers insertion order. To refresh a key: delete it and set it again. To evict: delete map.keys().next().value. It is fine for quick code, but interviewers usually want the explicit list.| Term | Meaning in easy words |
|---|---|
| LRU | Least Recently Used: evict the entry that was used longest ago |
| Hash map | A structure that finds a value by key in O(1) on average |
| Doubly linked list | Nodes with both prev and next pointers, so one node can be removed in O(1) |
| Sentinel (dummy) node | A fake HEAD or TAIL node that removes edge cases |
| Capacity | The maximum number of entries the cache holds |
| Hit / miss | The key was found in the cache / it was not |
| Eviction | Removing an entry to make room |
| Move to front | Marking an entry as most recently used |
| CLOCK | An approximate LRU using one bit per entry and a sweeping hand |
| Approximate LRU | Sampling a few entries and evicting the oldest of those |
| LFU | Least Frequently Used: evict the entry with the fewest accesses |
| Lock striping | Splitting a big lock into many small locks, one per shard |
| Choice | We gain | We lose |
|---|---|---|
| Exact LRU, one lock | Correct and simple | Contention under load |
| Sharded LRU | Parallel access | Eviction is only per shard |
| Approximate LRU / CLOCK | Cheap reads | Sometimes evicts a better item |
| LRU vs LFU | LRU adapts quickly | LFU holds steady hot keys better |
| Segmented LRU / TinyLFU | Resists scans | More code and tuning |
| Store value in node | One lookup | Large values make nodes heavy |