LRU Cache | (C++, Java, Python) |30 day Challenge | Day 24 | LeetCode

  Рет қаралды 11,368

Knowledge Center

Knowledge Center

Күн бұрын

Пікірлер: 55
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Share your interview experience. If it was asked to you in any programming interview?
@riteshsrivastava3447
@riteshsrivastava3447 3 жыл бұрын
that god it was wrongly put into capacity function else i was just getting frustrated ;-
@gokulnaathbaskar9808
@gokulnaathbaskar9808 2 жыл бұрын
Best video explanation of LRU Cache, short and clear!!!
@KnowledgeCenter
@KnowledgeCenter Жыл бұрын
Thanks. Glad you think so.
@bleakMidWinter8
@bleakMidWinter8 Жыл бұрын
Best solution to this problem till now. This was asked in Goldman Sachs last technical interview round I couldnt answer it properly
@jimwoodward7293
@jimwoodward7293 4 жыл бұрын
Your explanations are very clear and very easy to follow. You have impressive programming knowledge. Thanks for posting your videos.
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Glad it was helpful!
@jimwoodward7293
@jimwoodward7293 4 жыл бұрын
@@KnowledgeCenter They are very helpful. I'm considering joining your channel, what additional benefits are there to joining. Can I submit specific questions to you? I program in Python. Thanks.
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Wait for some time. I'll add a small video with all the details shortly. Better to join after that. And yes, adding specific Questions would be one of the benefits.
@jimwoodward7293
@jimwoodward7293 4 жыл бұрын
@@KnowledgeCenter That would be great -- thank you so much!
@revanthvenkateswar622
@revanthvenkateswar622 4 жыл бұрын
Fantastic. You go to great depths to put across the underlying concepts of the video. A positive learning experience came out of this lockdown. Thank you for this !!
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Glad it was helpful!
@harshthakkar7503
@harshthakkar7503 4 жыл бұрын
Best explaination 👏🏻
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Glad it was helpful!
@techspiritzz2268
@techspiritzz2268 Жыл бұрын
Thanks for the video! Referring to the C++ solution, it is accepted in Leetcode, however, the worst case time complexity of unordered_map's erase function is linear. So wouldn't that make the worst case of this algorithm O(n)?
@f3-faithfitnessfinance
@f3-faithfitnessfinance 4 жыл бұрын
In Java To find out the initially inserted element is that the only way or is there an alternate way?
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
You can implement it using LinkedList and Hash Map instead of LinkedHashMap. For LinkedHashMap you get the standard Map interface. So, you get the keyset() or entryset() then iterate. The thing that differentiates it from normal HashMap is internal implementation of preserving the order of insertion. The interface remains same. public class LinkedHashMap extends HashMap implements Map So, LinkedHashMap provides standard Map Interface.
@shubhamsood1406
@shubhamsood1406 3 жыл бұрын
One question I have for C++ implementation, for maintaining the order of [key, value] pair why you didn't use the Ordered Map ( map ) ? Please clear this doubt.
@inFamous16
@inFamous16 2 жыл бұрын
you don't require ordering here as long as you are storing the address of the page from list. Also using map will take O(logn) time for access and we have to do it in O(1), hence unordered_map is the most suitable here
@dhanashreegodase4445
@dhanashreegodase4445 2 жыл бұрын
thanks man!
@KnowledgeCenter
@KnowledgeCenter 2 жыл бұрын
Welcome.
@tomasmachacek2468
@tomasmachacek2468 4 жыл бұрын
Hi, if .keySet().iterator().next() is pointing to the first key of the LinkedHashMap, how do I point to the last key? And also, why do we remove the first key when the first is the most-recently-used, or isn't it? Thank you!
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
First key is the key that is oldest. Last key is the most recent inserted key. So, we are removing first key. Also for getting the last key in linkedHashMap, we need to iterate till end. It will be O(n) opeartion. while (iterator.hasNext()) { last_key = iterator.next() } or _map.keySet().toArray()[_map.size() -1] public class LinkedHashMap extends HashMap implements Map So, it doesn't provide interface for LinkedList but rather Map.
@monojit104
@monojit104 4 жыл бұрын
Could you plz make a video for the LFU, leetcode 460?
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Sure. Will create it immediately after May challenge ends. Already 3 leetcode questions Queued based on other Coders' requests. Thanks for suggesting.
@rishisharma5249
@rishisharma5249 4 жыл бұрын
Nice video sir!!!
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Thank you!
@AshokKumar-sw9oc
@AshokKumar-sw9oc 4 жыл бұрын
Nice video , can u make a series of important questions asked by companies
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Thanks for suggesting. I had started a playlist for Google and Facebook. But I felt there are good overlaps. So, rather than having separate playlists, I should have Common playlist of good and/or popular questions. The LeetCode playlist in the channel was created with that intent. Will keep adding regularly to it. I also felt most of the good questions asked in interviews are available on LeetCode. So, I plan to enrich LeetCode playlist itself. Any suggestions on this, or it looks fine??
@poojaagarwal1480
@poojaagarwal1480 4 жыл бұрын
@@KnowledgeCenter Yes that will be good! Please keep uploading solutions to leetcode problems. This keeps us going.
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
@@poojaagarwal1480 Sure. And make sure to try the problems first yourself and after that only refer videos like the ones on this channel or other channels, just to look for other approaches, or compare if there are better approaches available, or you are stuck for some time. Good Luck... Happy Learning...
@poojaagarwal1480
@poojaagarwal1480 4 жыл бұрын
@@KnowledgeCenter Yes Thank you
@aditgulia
@aditgulia 4 жыл бұрын
For java code directly go to 20:24
@siddarthvaranasi1175
@siddarthvaranasi1175 3 жыл бұрын
Can you please make a video on the TTL variant of the same problem? Thanks!
@elrey0801
@elrey0801 4 жыл бұрын
sir, I have a question. Is that erase operation in list O(1) or O(n), since it need to traverse through the linked list to find the element to be deleted?
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
We are not storing index, but iterator itself, we have direct pointer to the node. We don't need to start from beginning. unordered_map _map; So, you see we are storing list::iterator, which is pointing to the node. No traversal required. So, O(1).
@elrey0801
@elrey0801 4 жыл бұрын
​@@KnowledgeCenter thanks for your detailed explanation!
@anonymous_upsc_asprnt
@anonymous_upsc_asprnt 4 жыл бұрын
I have doubt when 1 was added 2nd time after that why it's not put in cache when it was not present there.
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
In this implementation of LRU get(key) calls donot insert a new (key,value) into the cache, since value info is not present in get() call. So, only key&value are inserted in put(k,v) calls. Though you are correct. If the constraint of (key,value) is not there as imposed by LeetCode for this problem, and if we are just storing keys, and not values, get(key) should also insert keys into cache, if it is a cache miss. So, in short the answer to your question is get(key) doesn't have value info, so no new insertion in get() calls here.
@anonymous_upsc_asprnt
@anonymous_upsc_asprnt 4 жыл бұрын
Thanks
@rishikeshkumarsingh3283
@rishikeshkumarsingh3283 4 жыл бұрын
waiting for the video from the morning
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Hope you enjoyed it!
@ketanlalcheta4558
@ketanlalcheta4558 3 жыл бұрын
Awesome playlist... thanks for this... I have a question on C++ implementation of get function.How it gets compiled is making me confused.. keys.erase will need iterator of list as keys is of type list. But you are passing _map[key].second Isn't this a pair of int and list iterator?
@inFamous16
@inFamous16 2 жыл бұрын
bro _map[key] itself is giving you the access of the value of _map for the key and then doing .second, you are getting access to the second element of the pair.
@RishiRaj-dl1mg
@RishiRaj-dl1mg 4 жыл бұрын
Awssom video sir. When I'm using keys.end()then it's giving compile error. So what's the difference between .end() and . back ()
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
end() returns iterator pointing to 1 position after the last element. back() returns the last element. Both are different things.
@RishiRaj-dl1mg
@RishiRaj-dl1mg 4 жыл бұрын
@@KnowledgeCenter thanks sir
@tarunsingh3687
@tarunsingh3687 4 жыл бұрын
Why can't we use deque instead of list in c++? Using deque gives an error. Please REPLY.
@riteshsrivastava3447
@riteshsrivastava3447 3 жыл бұрын
sir a strange question , actually i was wondering if people does this kind of questions at one go ? bcz i couldn think it ... i had to watch vedio for most of the standard questions .
@adityasrivastava7956
@adityasrivastava7956 3 жыл бұрын
+1
@TheMsnitish
@TheMsnitish 4 жыл бұрын
are you a software engineer ?
@PradeepKumar-so5wq
@PradeepKumar-so5wq 4 жыл бұрын
what does map[key].second=keys.begin(); saving ?
@arpit_singh10
@arpit_singh10 4 жыл бұрын
position of that key in the List
@arpanpandey5869
@arpanpandey5869 2 жыл бұрын
does anyone else get TLE for c++ code submitted recently ?
@piyushanand6664
@piyushanand6664 3 жыл бұрын
Why is this not working anymore? 14/21 test cases. class LRUCache { int cap; listls; unordered_mapmp; public: LRUCache(int capacity) { cap = capacity; } int get(int key) { if(mp.find(key)!=mp.end()) { ls.erase(mp[key].second); ls.push_front(key); mp[key].second = ls.begin(); return mp[key].first; } return -1; } void put(int key, int value) { if(mp.find(key)!=mp.end()) { ls.erase(mp[key].second); ls.push_front(key); mp[key]={value,ls.begin()}; }else { if(ls.size()==cap) { mp.erase(ls.back()); ls.pop_back(); } } ls.push_front(key); mp[key]={value,ls.begin()}; } };
LRU Cache - Twitch Interview Question - Leetcode 146
17:49
NeetCode
Рет қаралды 303 М.
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 34 МЛН
Мама у нас строгая
00:20
VAVAN
Рет қаралды 12 МЛН
Thank you Santa
00:13
Nadir Show
Рет қаралды 48 МЛН
LeetCode 146. LRU Cache (Algorithm Explained)
18:00
Nick White
Рет қаралды 119 М.
L18. Implement LRU Cache
24:56
take U forward
Рет қаралды 65 М.
Implement LRU Cache | Leetcode
17:05
take U forward
Рет қаралды 280 М.
Implement LRU cache
12:25
Techdose
Рет қаралды 126 М.
2 Years of C++ Programming
8:20
Zyger
Рет қаралды 7 М.
LRU Cache | Leetcode 146 | C++ Solution
18:02
Paritosh Wankhade
Рет қаралды 832
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 34 МЛН