Lesson 22 of 86BeginnerPro Interactive Lesson

Least Recently Used Cache

Track the recency order after a lookup or insertion to identify the eviction candidate.

Core Intuition & How It Works

An LRU (Least Recently Used) cache evicts the least recently accessed key when full. Combining a hash map for O(1) key-to-node lookup with a doubly linked list for O(1) recency promotion makes both get and put run in O(1) time.

In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.

Time & Space Complexity

MetricComplexity
Time ComplexityO(1) get and put
Space ComplexityO(capacity)
Topic ChapterChapter 4: Linked Lists
Difficulty LevelBeginner

Key Pattern to Recognize

Store key inside each list node so that when the tail node is evicted, you can also delete its entry from the hash map in O(1).

Common Pitfalls & Edge Cases

Forgetting to promote an existing key on put() updates, or failing to delete the evicted node’s key from the hash map.

Real-World & Technical Interview Applications

CPU and OS page caches, CDN edge caching, database buffer pools, and memoization layers.

Unlock the full course

All 86 lessons across 15 chapters

View pricing

Delete account

This permanently deletes your account and ends any Pro access.