AfterAcademy Tech
•
13 Oct 2020

Difficulty: Hard
Asked in: Amazon, Microsoft, Adobe, Google
Problem Description
Design and implement a data structure for Least Recently Used(LRU) cache.
Problem Note:
get() and put()get(key): Finds and returns the value if the key exists in the cache. If the key is not present in the cache, get(key) returns -1put(key, value): Inserts new key, if it is not present in the cache. If the cache is filled to capacity, it must remove the least recently used entry.O(1) time complexityget() and put()Example
cache = LRUCache(3)
cache.put(1,1)
cache.put(2,2)
cache.put(1,3)
cache.get(1) ---> returns 3
cache.put(3,4)
cache.put(4,3) // removes key 2
cache.get(2) ---> returns -1
Input Format:
N and C, the total number of queries and the cache size.N lines has a query of either type 1(put) or type 2(get).1 k v, where k is key and v is value2 k, where k is key whose value is to be fetched.For example, the input for the above example will be:
7 3
1 1 1
1 2 2
1 1 3
2 1
1 3 4
1 4 3
2 2
A Least Recently Used (LRU) Cache organizes items in order of use, allowing you to quickly identify which item hasn’t been used for the longest amount of time.
An LRU cache is often implemented by using a doubly-linked list(DLL) with a hash map.
The node structure used for the doubly linked list will be as follows:
class Node {
int key
int value
Node pre
Node next
public Node(int key, int value) {
this.key = key
this.value = value
}
}
To implement an LRU, we will move with the straightforward idea of using a doubly-linked list that will represent itself as an LRU cache. Corresponding to each item of the linked list, there will be a key existing in the hashmap.
We make the doubly linked list to always function such that the front node will always be the least recently used item. Now, we will be using two functions:
Using a doubly linked list will help us to push and pop the elements from the front and rear of the linked list in constant time and so DLL is preferred.
Solution Steps
3. If the item fails to be in the hash table on "get" operation, we have a cache miss and so we need to return -1.
4. For every "put" operation load the item into the LRU cache.
Pseudo Code
class LRUCache {
HashMap(Integer, Node) map
int capicity, count
Node head, tail
public LRUCache(int capacity) {
this.capicity = capacity
map = new HashMap()
// create dummy nodes to point head and tail
head = new Node(0, 0)
tail = new Node(0, 0)
head.next = tail;
tail.pre = head
head.pre = null
tail.next = null
count = 0
}
void deleteNode(Node node) {
node.pre.next = node.next
node.next.pre = node.pre
}
void addNodeToHead(Node node) {
node.next = head.next
node.next.pre = node
node.pre = head
head.next = node
}
int get(int key) {
if (key exist in map) {
Node node = map.get(key)
int result = node.value
deleteNode(node)
addNodeToHead(node)
return result
}
return -1
}
void put(int key, int value) {
if (key exist in map) {
Node node = map.get(key)
node.value = value
deleteNode(node)
addNodeToHead(node)
}
else {
Node node = new Node(key, value)
map.put(key, node)
if (count < capicity) {
count = count + 1
addNodeToHead(node)
}
else {
map.remove(tail.pre.key)
deleteNode(tail.pre)
addNodeToHead(node)
}
}
}
}
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(1)(How?)
Critical Ideas To Think
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
AfterAcademy Tech
In this blog, we will learn about the concept of Virtual Memory and we will also see how this concept of Virtual Memory can be implemented.

AfterAcademy Tech
This is an interview problem asked in companies like Microsoft, Amazon, Adobe, Flipkart, etc. Here, we need to implement Queue data structure using another data structure Stack. This problem is meant for testing the data structure knowledge.

Janishar Ali
Implement JSON Web Token (JWT) Authentication using AccessToken and RefreshToken In this blog, we will learn about the concepts of JsonWebToken (JWT) based authentication in Node.js. We will also cover all the edge cases of the security involved in token handling.

AfterAcademy Tech
Tree Data Structure supports various operations such as insertion, deletion and searching of elements along with few others. This blogs discussed those operations along with explaining its applications.
