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

Поділитися
Вставка
  • Опубліковано 19 вер 2024
  • Complete Playlist LeetCode Solutions: • LeetCode Solutions | L...
    *** Best Books For Data Structures & Algorithms for Interviews:*********
    1. Cracking the Coding Interview: amzn.to/2WeO3eO
    2. Cracking the Coding Interview Paperback: amzn.to/3aSSe3Q
    3. Coding Interview Questions - Narasimha Karumanchi: amzn.to/3cYqjkV
    4. Data Structures and Algorithms Made Easy - N. Karumanchi: amzn.to/2U8FrDt
    5. Data Structures & Algorithms made Easy in Java - N. Karumanchi: amzn.to/2U0qZgY
    6. Introduction to Algorithms - CLR - Cormen, Leiserson, Rivest: amzn.to/2Wdp8rZ
    *****************************************************************************
    LeetCode 30 day Challenge | Problem 24 | LRU Cache | 24 April,
    Facebook Coding Interview question,
    google coding interview question,
    leetcode,
    lru cache python,
    lru cache java,
    lru cache c++,
    #Facebook #CodingInterview #LeetCode #30DayChallenge #Google #LRU #Amazon

КОМЕНТАРІ • 55

  • @KnowledgeCenter
    @KnowledgeCenter  4 роки тому +1

    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 ;-

  • @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 роки тому +2

    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 роки тому +1

      @@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 роки тому +1

      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 роки тому +1

      @@KnowledgeCenter That would be great -- thank you so much!

  • @gokulnaathbaskar9808
    @gokulnaathbaskar9808 Рік тому +1

    Best video explanation of LRU Cache, short and clear!!!

  • @revanthvenkateswar622
    @revanthvenkateswar622 4 роки тому +2

    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 !!

  • @harshthakkar7503
    @harshthakkar7503 4 роки тому +2

    Best explaination 👏🏻

  • @dhanashreegodase4445
    @dhanashreegodase4445 2 роки тому +1

    thanks man!

  • @techspiritzz2268
    @techspiritzz2268 10 місяців тому

    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)?

  • @shubhamsood1406
    @shubhamsood1406 3 роки тому +1

    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

  • @AshokKumar-sw9oc
    @AshokKumar-sw9oc 4 роки тому +2

    Nice video , can u make a series of important questions asked by companies

    • @KnowledgeCenter
      @KnowledgeCenter  4 роки тому +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 роки тому +2

      @@KnowledgeCenter Yes that will be good! Please keep uploading solutions to leetcode problems. This keeps us going.

    • @KnowledgeCenter
      @KnowledgeCenter  4 роки тому +1

      @@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

  • @rishisharma5249
    @rishisharma5249 4 роки тому +1

    Nice video sir!!!

  • @f3-faithfitnessfinance
    @f3-faithfitnessfinance 4 роки тому +2

    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.

  • @monojit104
    @monojit104 4 роки тому +2

    Could you plz make a video for the LFU, leetcode 460?

    • @KnowledgeCenter
      @KnowledgeCenter  4 роки тому +1

      Sure. Will create it immediately after May challenge ends. Already 3 leetcode questions Queued based on other Coders' requests. Thanks for suggesting.

  • @rishikeshkumarsingh3283
    @rishikeshkumarsingh3283 4 роки тому +2

    waiting for the video from the morning

  • @tomasmachacek2468
    @tomasmachacek2468 4 роки тому +2

    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.

  • @anonymous_upsc_asprnt
    @anonymous_upsc_asprnt 4 роки тому +2

    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 роки тому +2

      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 роки тому +1

      Thanks

  • @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.

  • @siddarthvaranasi1175
    @siddarthvaranasi1175 3 роки тому

    Can you please make a video on the TTL variant of the same problem? Thanks!

  • @RishiRaj-dl1mg
    @RishiRaj-dl1mg 4 роки тому +1

    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

  • @elrey0801
    @elrey0801 4 роки тому +1

    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 роки тому +3

      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 роки тому +1

      ​@@KnowledgeCenter thanks for your detailed explanation!

  • @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 .

  • @aditgulia
    @aditgulia 4 роки тому +1

    For java code directly go to 20:24

  • @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()};
    }
    };

  • @tarunsingh3687
    @tarunsingh3687 4 роки тому

    Why can't we use deque instead of list in c++? Using deque gives an error. Please REPLY.

  • @TheMsnitish
    @TheMsnitish 4 роки тому +1

    are you a software engineer ?

  • @PradeepKumar-so5wq
    @PradeepKumar-so5wq 4 роки тому

    what does map[key].second=keys.begin(); saving ?

    • @arpit_singh10
      @arpit_singh10 4 роки тому +1

      position of that key in the List

  • @arpanpandey5869
    @arpanpandey5869 Рік тому

    does anyone else get TLE for c++ code submitted recently ?