mediumDesignHash TableLinked ListLinked List

LRU Cache

mediumTime: O(1)Space: O(capacity)

Signals to notice

O(1) get and putevict least recently usedordered access tracking

Brute force first

List + linear scan to find/evict — per operation. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

The key insight

Hash map (key → node) + doubly linked list (ordered by recency). Get: look up in map, move to front. Put: add to front, evict from back if over capacity. Both. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

What must stay true

The doubly linked list maintains access order: most recent at front, least recent at back. The hash map gives access to any node for direct removal and reinsertion. As long as that statement keeps holding, you can trust the steps built on top of it.

Easy way to go wrong

Using a singly linked list — you need removal from the middle, which requires a doubly linked list with previous pointers. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Linked List Pattern