Design a data structure for LRU Cache. It should support the following operations: get and set.
get(key) – Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) – Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
// Let’s say we have a LRU cache of capacity 2.
LRUCache cache = new LRUCache(2);
cache.set(1, 10); // it will store a key (1) with value 10 in the cache.
cache.set(2, 20); // it will store a key (2) with value 20 in the cache.
cache.get(1); // returns 10
cache.set(3, 30); // evicts key 2 and store a key (3) with value 30 in the cache.
cache.get(2); // returns -1 (not found)
cache.set(4, 40); // evicts key 1 and store a key (4) with value 40 in the cache.
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 30
cache.get(4); // returns 40
Asked In: Adobe, Hike and many more companies.
1. Brute-force Approach:
We will keep an array of Nodes and each node will contain the following information:
The size of the array will be equal to the given capacity of cache.
(a) For get(int key): We can simply iterate over the array and compare the key of each node with the given key and return the value stored in the node for that key. If we don’t find any such node, return simply -1.
Time Complexity: O(n)
(b) For set(int key, int value): If the array if full, we have to delete one node from the array. To find the LRU node, we will iterate through the array and find the node with least timeStamp value. We will simply insert the new node (with new key and value) at the place of the LRU node.
If the array is not full, we can simply insert a new node in the array at the last current index of the array.
Time Complexity: O(n)
2. Optimized Approach:
The key to solve this problem is using a double linked list which enables us to quickly move nodes.
The LRU cache is a hash map of keys and double linked nodes. The hash map makes the time of get() to be O(1). The list of double linked nodes make the nodes adding/removal operations O(1).
Java Code using Doubly Linked List and HashMap:
Going to test the LRU Cache Implementation Going to set the (key, value) : (1, 10) Going to set the (key, value) : (2, 20) Got the value : 10 for the key: 1 Value for the key: 1 is 10 Going to set the (key, value) : (3, 30) Did not get any value for the key: 2 Value for the key: 2 is -1 Going to set the (key, value) : (4, 40) Did not get any value for the key: 1 Value for the key: 1 is -1 Got the value : 30 for the key: 3 Value for the key: 3 is 30 Got the value : 40 for the key: 4 Value for the key: 4 is 40
Another implementation in Java using LinkedHashMap:
removeEldestEntry() is overridden to impose a policy for removing old mappings when size goes beyond capacity.
Going to test the LRU Cache Implementation Going to set the (key, value) : (1, 10) Going to set the (key, value) : (2, 20) Going to get the value for the key : 1 Value for the key: 1 is 10 Going to set the (key, value) : (3, 30) Going to get the value for the key : 2 Value for the key: 2 is -1 Going to set the (key, value) : (4, 40) Going to get the value for the key : 1 Value for the key: 1 is -1 Going to get the value for the key : 3 Value for the key: 3 is 30 Going to get the value for the key : 4 Value for the key: 4 is 40
You can find the C++ code here
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- LRU Cache in Python using OrderedDict
- Locality of Reference and Cache Operation in Cache Memory
- Design an efficient data structure for given operations
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Data Structure design to perform required operations
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- Least Frequently Used (LFU) Cache Implementation
- Multilevel Cache Organisation
- Write Through and Write Back in Cache
- Design a structure which supports insertion and first non-repeating element in O(1) time
- RTL (Register Transfer Level) design vs Sequential logic design
- Difference between Data Scientist, Data Engineer, Data Analyst
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Applications of linked list data structure
- Remove duplicates from unsorted array using Map data structure
- Stack Data Structure (Introduction and Program)
- Difference between Data Warehousing and Data Mining
- Introduction to Data Structures | 10 most commonly used Data Structures
- Primitive data type vs. Object data type in Java with Examples
- Protein structure prediction in Cloud Computing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : gooood