Watch an LRU cache serve 12 requests, move each hit to the front and evict the least recently used item. Change the capacity, and see O(1) get and put in code.
About this algorithm
An LRU cache (least recently used) holds a fixed number of items and, when it runs out of room, evicts the one that has gone longest without being used. The bet is that what you touched a moment ago you will touch again soon, and what you have ignored for a while you can live without. It is the default eviction rule in browsers, databases and operating systems, and the LeetCode problem "LRU Cache" asks you to build it with every operation in constant time.
Choose LRU when recent use predicts the next use, which covers most real workloads: a user flipping between a few tabs, a web server serving the same hot pages, a program looping over a working set that fits in memory. FIFO is cheaper but ignores hits, so it can evict a page it just used. LFU remembers long-term popularity instead of recency, which helps when a few items stay popular for hours and hurts when interest shifts. MRU is the right call only for a loop that scans more data than the cache can hold. Clock approximates LRU when moving an item on every hit costs too much.
The list keeps keys in order of last use, most recent at the head and least recent at the tail. The map points from each key straight to its list node, so you never search the list.
Find the node through the map, unlink it from where it sits, and relink it at the head. Both ends of a doubly linked list can be reached and changed in constant time, which is why this pairing works.
If the cache is at capacity, the tail node is the least recently used key. Remove it from the list and delete it from the map.
Create a node for the requested key, put it at the head, and record it in the map. It is now the most recently used item.
The page requests 1 2 3 4 1 2 5 1 2 3 4 5 with four slots, and draws the most recent key on the left. The first four requests miss and fill the cache: 4 3 2 1. Request 1 hits and jumps to the front, giving 1 4 3 2, then 2 hits, giving 2 1 4 3. Now 5 misses with the cache full. The key at the tail is 3, the one untouched for longest, so 3 is evicted and the cache reads 5 2 1 4. Requests 1 and 2 hit again because LRU kept them. Then 3 misses and evicts 4, 4 misses and evicts 5, and 5 misses and evicts 1. The run ends with 4 hits out of 12, a 33% hit rate, twice what FIFO manages on the same requests.
A JavaScript Map and a Python OrderedDict both remember insertion order. To mark a key as used, delete it and set it again so it moves to the newest end; to evict, remove the oldest key, the first one the map yields. The code panel on this page uses that shortcut. In an interview you may be asked to write the linked list yourself.
Time: the map finds a node in constant time and the doubly linked list unlinks and relinks it in constant time, so no operation ever walks the list. Space: one map entry and one list node per cached key.