Introduction to LRU Cache (original) (raw)
Last Updated : 15 Jun, 2026
Least Recently Used (LRU) is a cache eviction policy that keeps track of how recently data is used. When the cache has no space for new data, it removes the item that has not been accessed for the longest time and stores the new item instead.
- Frequently accessed data is given higher priority in the cache because it has a strong chance of being needed again soon.
- Data that stays unused for a long time is assigned lower priority and is removed first when the cache becomes full.
- **Example: If a cache can store 3 pages and the access sequence is A -> B -> C -> A -> D, page B is evicted because it is the least recently used (LRU) item.

Operations on LRU Cache
These operations help manage data efficiently in the cache by storing, accessing, updating, and removing items based on their recent usage.
- **LRU Cache(capacity c): Initializes the LRU cache with a fixed capacity
c. - **get(key): Returns the value associated with the given key if it exists in the cache; otherwise, returns
-1. The accessed item is marked as the most recently used. - **put(key, value): Inserts a new key-value pair or updates the value of an existing key. If the cache exceeds its capacity, the least recently used item is removed.
Working of LRU Cache
Let's suppose we have an LRU cache of capacity 3, and we would like to perform the following operations:
- put (key=1, value=A) into the cache
- put (key=2, value=B) into the cache
- put (key=3, value=C) into the cache
- get (key=2) from the cache
- get (key=4) from the cache
- put (key=4, value=D) into the cache
- put (key=3, value=E) into the cache
- get (key=4) from the cache
- put (key=1, value=A) into the cache
The above operations are performed one after another as shown in the image below:

**Explanation of Each Operation
- **put(1, A): The cache is empty, so
{1:A}is added directly and becomes the most recently used item. - **put(2, B): The cache still has space, so
{2:B}is added. It now becomes the most recently used item, while{1:A}moves to lower priority. - **put(3, C): The cache reaches full capacity after adding
{3:C}.
Priority order:{3:C} → {2:B} → {1:A} - **get(2): Returns value
Band marks key2as the most recently used item.
New priority order:{2:B} → {3:C} → {1:A} - **get(4): Key
4is not present in the cache, so the operation returns-1. - **put(4, D): The cache is full, so the least recently used item
{1:A}is removed.{4:D}is added as the most recently used item.
New priority order:{4:D} → {2:B} → {3:C} - **put(3, E): Key
3already exists, so its value is updated fromCtoE.
It also becomes the most recently used item.
New priority order:{3:E} → {4:D} → {2:B} - **get(4): Returns value
Dand updates key4as the most recently used item.
New priority order:{4:D} → {3:E} → {2:B} - **put(1, A): The cache is full again, so the least recently used item
{2:B}is removed.{1:A}is inserted as the most recently used item.
Final priority order:{1:A} → {4:D} → {3:E}
Designing a LRU Cache
**Input:[LRUCache(2), put(1,1), put(2,2), get(1), put(3,3), get(2)]
**Output: [1, -1]
**Example: Cache Capacity = 2
| Operation | What Happens |
|---|---|
| put(1,1) | Add key 1 → Cache: [1] |
| put(2,2) | Add key 2 → Cache: [1, 2] |
| get(1) | Returns 1; key 1 becomes most recently used → Cache: [2, 1] |
| put(3,3) | Cache full; remove key 2 (LRU), add key 3 → Cache: [1, 3] |
| get(2) | Returns -1 (not found) |
**Output: [1, -1]
**Reason: Key 2 was the least recently used item, so it was removed when key 3 was inserted.
LRU Cache Implementation Methods
- **Array-Based Implementation: Stores key-value pairs with timestamps in an array. Since searching and updating require scanning the array, both get() and put() operations take O(n) time.
- **Using Hashing and Heap: Hashing improves lookup speed, while a heap helps identify the least recently used item. However, some operations still require O(log n) time, so O(1) performance is not achieved.
- **HashMap + Doubly Linked List (Optimal): Uses a hash map to locate cache entries in O(1) time and a doubly linked list to maintain usage order. Both get() and put() operations run in O(1) time, making this the standard and most efficient implementation of an LRU cache.
Refer Design LRU Cache for details of all approaches.
Efficient Solution - Using Doubly Linked List and Hashing
The LRU (Least Recently Used) cache is commonly implemented using a combination of a hash map and a doubly linked list to perform insertion, deletion, and access operations efficiently.
- New key-value pairs are inserted at the head of the doubly linked list, making them the most recently used items. If a key is accessed or updated, its node is moved to the head of the list.
- The doubly linked list maintains priority based on usage. Nodes near the head are recently used, while nodes near the tail are least recently used.
- When the cache reaches maximum capacity, the node at the tail of the list is removed because it represents the least recently used item. The corresponding entry is also removed from the hash map to free space for new data.
Refer LRU cache implementation using Doubly Linked List and Hashing for details
Complexity Analysis of the Efficient Solution
The efficient implementation of an LRU Cache uses a combination of a Hash Map and a Doubly Linked List to perform cache operations in constant time.
Time Complexity
- **put() Operation: O(1): Inserting or updating a key-value pair takes constant time.
- **get() Operation: O(1): Accessing a key and updating its recent usage also takes constant time.
Auxiliary Space
- **Space Complexity: O(c): Where
cis the capacity of the cache.
Real-World Application
LRU Cache is commonly used in systems where recently accessed data needs to be retrieved quickly.
- Database systems: for faster query results
- Operating systems: to reduce page faults
- Text editors/IDEs: to store frequently used files or code snippets
- Network routers/switches: to improve efficiency in data handling
- Compiler optimizations: to improve performance
- Text prediction/autocomplete: for faster suggestions