How to use the LRU Cache Visualizer
- 1. Set it upChoose a capacity (1–8) and type operations: put key value or get key.
- 2. RunPress Run.
- 3. ReplayStep through: the most recent item is on the left, the next to be evicted on the right.
Worked example
With capacity 3, the sample puts 1, 2 and 3, so the order is 3, 2, 1. get 1 is a hit and moves 1 to the front: 1, 3, 2. put 4 finds the cache full and evicts 2, the least recently used. get 2 is then a miss. get 3 is a hit, put 1 Z updates key 1 and moves it to the front, and get 4 is a hit. The result: 3 hits, 1 miss and 1 eviction, with the final order 4, 1, 3.
How a real LRU cache is O(1)
A hash map finds a key's node instantly, and a doubly linked list keeps nodes in order of use. On get, the node is unlinked and moved to the front; on put, a new node goes to the front and, if the cache is full, the tail node is removed and its key deleted from the map. Every step is constant time. In Java, LinkedHashMap with access order and removeEldestEntry does this; in Python, OrderedDict with move_to_end, or functools.lru_cache for functions.
Where LRU is used
- Browser and CDN caches, database buffer pools and operating-system page caches.
- Memoising expensive function calls.
- A very common design question: LeetCode's LRU Cache problem asks for exactly this structure.
Frequently asked questions
Does put on an existing key count as use?
Yes. It updates the value and moves the key to the front, as most implementations do.
What's the difference from LFU?
LFU evicts the least frequently used item; LRU evicts the one unused for the longest time.
Can keys be text?
Yes. Keys and values can be any text without spaces in the key.
Is the LRU cache visualizer free?
Yes. You can use the LRU cache visualizer without signing up. The first time you run it, we ask once for your name and WhatsApp number; after that the LRU cache visualizer and every other Uncodemy tool open straight away.