ASAPUtils Logo ASAPUtils
Week 5

LRU Cache

Design an LRU Cache by combining O(1) hash lookup with O(1) doubly linked recency updates, including C++ and JavaScript implementations, sentinel-node invariants, eviction logic, complexity, and an interactive cache trace.

medium Linked Lists O(1) average per operation time · O(capacity) space Open on LeetCode ↗

The problem

Implement a fixed-capacity cache whose get and put operations are O(1), evicting the least recently used key whenever a new insertion exceeds capacity.

capacity 2: put(1,1), put(2,2), get(1), put(3,3) evicts key 2

Related Problems