Skip to content

visual walkthrough

LRU Cache

MediumHash Map + Doubly Linked ListReported at: AmazonMetaMicrosoft+17

One of the most-asked design questions at every FAANG company.

Solve on LeetCode

The idea

An LRU cache needs two things fast: finding a key, and knowing which entry was used longest ago.

A hash map does the first. A doubly linked list in order of use does the second: move a node to the front when it's used, and evict from the back. The map stores nodes, so a node can be unlinked from the middle without searching.

Complexity

approachtimespace
Ordered list, searchedO(n) per opO(n)
Hash map + doubly linked listO(1) per opO(capacity)

More walkthroughs