Implement An LRU Cache - The LRU Cache Eviction Policy ('LRU Cache' on LeetCode)

preview_player
Показать описание

📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions

Question: Implement an LRU Cache.

It is a cache replacement policy that says to evict the least recently used item in the cache.

Every time an item is used it goes to the "front" of the cache since it has been used and has priority.

Items that are not used will go to the "end" of the cache eventually and get evicted since they are the least used items, they never got a bump up by being used.

What is a Cache?

A cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere.

What Is An LRU Cache?

So a LRU Cache is a storage of items so that future access to those items can be serviced quickly and an LRU policy is used for cache eviction.

The Constraints/Operations

Lookup of cache items must be O(1)
Addition to the cache must be O(1)
The cache should evict items using the LRU policy

The Approach

There are many ways to do this but the most common approach is to use 2 critical structures: a doubly linked list and a hashtable.

Our Structures

Doubly Linked List: This will hold the items that our cache has. We will have n items in the cache.

This structure satisfies the constraint for fast addition since any doubly linked list item can be added or removed in O(1) time with proper references.

Hashtable: The hashtable will give us fast access to any item in the doubly linked list items to avoid O(n) search for items and the LRU entry (which will always be at the tail of the doubly linked list).

This is a very common pattern, we use a structure to satisfy special insertion or remove properties (use a BST, linked list, etc.) and back it up with with a hashtable so we do not re-traverse the structures every time to find elements.

Complexities

Time

Both get and put methods are O( 1 ) in time complexity.

Space

We use O( n ) space since we will store n items where n ist the capacity of the cache.

++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++

This question is number 13.3 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.
Рекомендации по теме
Комментарии
Автор

Table of Contents: (I'm screaming in this video. I know. I am sorry.)

Messing Around 0:00 - 0:23
Problem Introduction 0:23 - 0:36
What Does LRU (Least Recently Used) Mean? 0:36 - 1:10
Short Example of The LRU Policy 1:10 - 2:28
What Is A Cache? 2:28 - 2:53
What Is An LRU Cache? 2:53 - 3:12
The Properties of An LRU Cache 3:12 - 6:44
LRU Cache Operations Walkthrough 6:44 - 12:18
Summarizing The Ideas 12:18 - 12:53
More Subscriber Begging 12:53 - 13:12

Notes:

10:52 -> We need the nodes to hold their own keys. This matters when the LRU entry is evicted when capacity is surpassed. We just pop the item from the doubly linked list's end BUT I made the mistake to say the nodes won't need keys. They will need their respective key. We need the removed node's key to remove it from the hashtable since it is now out of the cache completely.

Comments:

Yes. I am screaming. I know. I messed up the first like 30 videos because I was still learning audio.

The code is in the description.

Note, we could also use Java's LinkedHashMap, but I choose to use this code example since it takes all that abstraction away so you see all of the critical operations that need to be taken care of if you were to really implement this.

BackToBackSWE
Автор

This channel is underrated, dude u r really awesome !

MostafaAliMansour
Автор

just answered this question yesterday in an interview, bless your channel. Thanks to you, I was able to stumble upon the optimal solution a lot quicker than if I was working on my own!

maripaz
Автор

I feel so lucky that I found your channel. Your videos are so easy to understand. You're awesome!

ANGELINK
Автор

Cannot appreciate enough how well you explain the problem, and not just read out the problem statement and code! Sometimes just understanding the code is required but often I need an in-depth understanding of the question and what was the thought process etc. Whenever I come across a question like that, I just hope that you would have made a video on it and come to your channel :)

srijaanand
Автор

Really great channel. The level of energy you have is insane. It motivates me more to put more energy to practice and prepare. very well explained and great content. you earned my subscription. Specially with the weather outside when you feel sleepy and dull and cant read more than a page, this is a must watch channel for anyone who is preparing for interviews in this season. Don't change your style :)

aditisaha
Автор

Great video, I love how you spent the entire video breaking down the concepts instead of showing any code.

TKNinja
Автор

Wow how is this channel so underrated. I feel like I found hidden treasure and I want others to also find it...

snowing
Автор

There's always a Back To Back SWE video to explain the leetcode problem that confused me.

kevin-lghs
Автор

I have my campus recruitment in a month, and your entire channel is on my revision list. Dude, I can't thank you enough for making these high-quality videos.

sddhrtha
Автор

Your energy and spirit is unmatched. Love ya man

zahranajib
Автор

Your channel is brilliant. I love it so much that I told all my friends about this channel. The clarity in your explanation filled with the energy and passion to teach is just fantastic 🙌

lings
Автор

Thanks man, i literally spent 3 hours on this and finally get after watching this

amansinhparmar
Автор

I think u r not an employee u r a perfect teacher i have seen very few people like you sir awesome

shivakrishna
Автор

This channel is so underrated. Great work!

danni
Автор

I have not seen such a great explanation and code. You are just awesome man. God bless you!

keshavrastogi
Автор

I'm about a month into my DS&A study and so many data structures and principles just clicked. Thank you for this!!!

giannizamora
Автор

You are the best at what you are doing. Love the way you explain and makes things so easy. Its kind of motivating to me. Looking forward to more of your videos. God bless you.

anssha
Автор

Holy shoot~! I 've never thought about a double-linked list on this. Thank you for your clarification.

abcdeereijgfhd
Автор

You teach better than my professor. Thanks

Ana-vxvx