filmov
tv
Implementing LRU Cache - Common Interview problem - Leetcode

Показать описание
Caching is a method of storing data with the assumption that they will useful to process future requests faster.
There's always a limit to the amount of data that can be stored in the cache. When we hit the cache limit while we're trying to add a new item into the cache, we need to find a way to replace an existing item. Algorithms that perform this task are called cache replacement algorithms. In this video, I'm talking about a cache-replacement-algorithm called LRU/Least Recently Used cache. This is also a common interview problem in big tech companies like Google, Amazon, Facebook, Apple.
Here, I'll be showing you how to solve the Leetcode problem - LRU Cache
You need to know the data structures HashMap and Doubly linked lists. We use HashMaps for fast look ups from within cache.
We use doubly linked list to keep the order in the cache items were used. First of the list is always the most recently used item. Tail is the least recently used item. If we use one of the elements, we need to remove it from the list and add it to the front of the list. Removing and adding to front of a list can be efficiently done using doubly linked list, which is why we chose a doubly linked list. Whenever we have to add a new item to the cache but the cache capacity is already reached, we can remove the last element in the doubly linked list because that is the least recently used item.
References
There's always a limit to the amount of data that can be stored in the cache. When we hit the cache limit while we're trying to add a new item into the cache, we need to find a way to replace an existing item. Algorithms that perform this task are called cache replacement algorithms. In this video, I'm talking about a cache-replacement-algorithm called LRU/Least Recently Used cache. This is also a common interview problem in big tech companies like Google, Amazon, Facebook, Apple.
Here, I'll be showing you how to solve the Leetcode problem - LRU Cache
You need to know the data structures HashMap and Doubly linked lists. We use HashMaps for fast look ups from within cache.
We use doubly linked list to keep the order in the cache items were used. First of the list is always the most recently used item. Tail is the least recently used item. If we use one of the elements, we need to remove it from the list and add it to the front of the list. Removing and adding to front of a list can be efficiently done using doubly linked list, which is why we chose a doubly linked list. Whenever we have to add a new item to the cache but the cache capacity is already reached, we can remove the last element in the doubly linked list because that is the least recently used item.
References
Комментарии