LRU Cache - Explanation, Java Implementation and Demo

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • Explanation and implementation of the LRU (Least Recently Used) Cache in Java. We do a hands on coding using Java and also try to run our cache.
    This is also one of the very common interview questions asked in software engineering.
    Link to code: gist.github.co...

ความคิดเห็น • 18

  • @BhriguSrivastava
    @BhriguSrivastava  3 ปีที่แล้ว +1

    In the 'put' function, inside 'if(currentNode != null)' there should be a 'return' in the end of this block. This is because we don't need to create new node if the node with that key is already present.
    (or, put the remainig lines in that function in 'else' block).
    The code gist on GitHub is updated

  • @akhilsrivastava149
    @akhilsrivastava149 2 ปีที่แล้ว

    Very good explanation. Please load more videos.

  • @ee_32_farzanreza10
    @ee_32_farzanreza10 2 ปีที่แล้ว

    Very good explanation

  • @abhinavsinghvirsen
    @abhinavsinghvirsen 3 ปีที่แล้ว

    awesome explanation Keep doing

  • @gouravkumar7459
    @gouravkumar7459 3 ปีที่แล้ว

    Thanks for the explaination! i think in put function ,in block where currentNode !=null there should be return also.

    • @BhriguSrivastava
      @BhriguSrivastava  3 ปีที่แล้ว

      Yes Gourav, that is correct. Seems like I missed (actually I was supposed to put the remaining lines in that function in the 'else' block, but missed it). I've updated in the code gist in GitHub.

  • @dewendrapandey1145
    @dewendrapandey1145 2 ปีที่แล้ว

    when it refers 4 again order must change, so as for 2 if you don't change order, how will you decide which need to delete for new entry?

  • @gamopim
    @gamopim 2 ปีที่แล้ว +1

    hi! the UseCache code is available somewhere? thanks for the tips!

  • @akshitjain2906
    @akshitjain2906 3 หลายเดือนก่อน

    legend

  • @ankitgupta-ph4nk
    @ankitgupta-ph4nk 2 ปีที่แล้ว

    Just one question in my mind.. If we are adding a new element and capacity is available, then why we are adding it at the beginning??

    • @akdesai832
      @akdesai832 2 ปีที่แล้ว

      To make it recently used. Adding new one or accessing the existing key makes the key recently used and hence renew its expiry.

  • @rozersmith
    @rozersmith 2 ปีที่แล้ว

    Hey brother , there is a fundamental problem you have missed in the LRU .
    lets say we have 2->4 ->7 and again there is 4 which is requested we need to adjust the linked list again to 4->2->7 coz 4 is now recently used and the order should be retained
    lets say again 7 is hit , now the order will become 7->4>2 if you carefully observer now the least recently used is actually 2 - please fix this

    • @BhriguSrivastava
      @BhriguSrivastava  2 ปีที่แล้ว +1

      Hi, yes you are correct but the diagram shown in the video at 2:50 doesn't show the items in order. It's just random. When we're inserting 9 at 3:28, we're removing 4 from the memory because 4 is not recently used (but in the diagram it's present in the middle of list). It's handled in code, but while explaining LRU, I was not explaining how it'll be ordered internally if we use a list, but just showing all the items randomly.

  • @unknown-lv8io
    @unknown-lv8io 8 หลายเดือนก่อน

    sound is very low

  • @eshantkumargola
    @eshantkumargola 5 หลายเดือนก่อน

    Volume in this video is very feeble.

  • @howardchang7999
    @howardchang7999 2 ปีที่แล้ว

    i m a noob