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
| Metric | Complexity |
|---|---|
| Time Complexity | O(1) get and put |
| Space Complexity | O(capacity) |
| Topic Chapter | Chapter 4: Linked Lists |
| Difficulty Level | Beginner |
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.