Interview Question: Start of Loop in a Linked List

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • This famous interview question is about reporting if a linked list has a loop, and then finding it's start. We use the 'Hare and Tortoise' approach, where two pointers moving at different speeds meet at surprising positions.
    (Excellent) Answer Link by CEGRD:
    stackoverflow.c...
    #linked-list #interview #puzzle

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

  • @shloksingh10
    @shloksingh10 7 ปีที่แล้ว +329

    hey i have an alternate solution for finding head.
    after hare and tortoise meet. we will make hare pointer stop at that point and make tortoise pointer move till it reaches hare pointer again and count number on nodes.
    this will give us length of cycle. ( how many nodes are in cycle). lets denote dis with c.
    then just take 2 pointers p1 and p2 at c distance apart.. and move both at SAME speed till p1 and p2 meets. that point will be head.

    • @gkcs
      @gkcs  7 ปีที่แล้ว +45

      Hey Shlok, this will also work. Nice alternative :-)

    • @futurezing
      @futurezing 7 ปีที่แล้ว +3

      Nice alternative. IMHO, that would increase the constant of the algorithm and also the code complexity. If you don't care about these, then it works.

    • @udaysaiphanindra3138
      @udaysaiphanindra3138 5 ปีที่แล้ว +3

      simple solution sir great

    • @bismeetsingh352
      @bismeetsingh352 5 ปีที่แล้ว

      @@DheerajKumar-hr8bf They can point anywhere. Make p1 point to start of list for example and p2 c distance apart.
      github.com/BismeetSingh/linkedlistoperations
      Let me know if any mistakes found.

    • @abhinavporwal1476
      @abhinavporwal1476 5 ปีที่แล้ว +2

      Hey, shlok . Thanks Man. Great Imagination!

  • @WilliamKurth
    @WilliamKurth 5 ปีที่แล้ว +31

    If you get asked this question in an interview and instantly respond with Tortoise and Hare you’ll likely get another question. I think some people forget the goal of interviews questions is often to see how you solve a problem you don’t already know... that being said, this is one of the better explanations I’ve seen for this algo

    • @gkcs
      @gkcs  5 ปีที่แล้ว +94

      Best to brush up on those acting skills then (Oh, find a loop in a linked list?! Gee, I wonder how....)

    • @deeproy7292
      @deeproy7292 4 ปีที่แล้ว +2

      @@gkcs nice thought...

    • @sachinmaurya3259
      @sachinmaurya3259 3 ปีที่แล้ว +2

      @@gkcs haha

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

      can you really end up figuring out haze and turtle by yourself for the first time you see this exercice ? I mean under pressure ?

    • @Marwan-oh4tk
      @Marwan-oh4tk 9 หลายเดือนก่อน

      @@splendidbeaver2027 definitely not, lol!

  • @jaatharsh
    @jaatharsh 4 ปีที่แล้ว +11

    in the end, he meant we get to the Starting point in the cycle since,
    Moving a Pointer from Head 'D' times = moving the pointer from the meeting point in cycle some constants times - K node
    Hence the solution works.
    Thanks, Gaurav this was really helpful.

  • @TheHotdogstand
    @TheHotdogstand 3 ปีที่แล้ว +7

    You're not only good at the CS part of things, you're also an amazing teacher! I kept asking a question in my head only to have you answer it right after. Thank you for this amazing lesson!

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

      Thank you 😁

  • @AkshayKumar-mo9fk
    @AkshayKumar-mo9fk 3 ปีที่แล้ว +3

    Thank god, finally someone explained the intuition behind this..
    You're just amazing 🙌

  • @bitbyte8177
    @bitbyte8177 4 ปีที่แล้ว +4

    Bro, I searched the whole internet to get the proof which I could understand. I wasted my two days in that search and couldn't find one which made sense to me or which I can sink the proof in. I watched your video twice. And I could finally sink it in. Thank you so much man! you saved me. Especially the last proof of how to make the pointers point to the starting node.

  • @ayushraj-zb6sv
    @ayushraj-zb6sv 2 ปีที่แล้ว +2

    most important thing is how to act like you have seen this question for first time in an interview :))

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

    It was hard wrap my head around this concept. Thank you for making this video and providing with a detailed explanation.

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

    Best Explanation. Thanks Gaurav for this.
    1) At the meeting point one pointer that starts from start covers D.
    2) Now at the same point when the next pointer start from meeting point trying to travel D, It will cover N*(circle Length) - k. so from the point it started at will be k steps before. And k steps before is actually the meeting point. So they both will meet at the meeting point.

  • @AbrarShariar
    @AbrarShariar 4 ปีที่แล้ว +5

    Love the energy of your teaching style!

  • @ananyamishra382
    @ananyamishra382 9 หลายเดือนก่อน

    it is evident that he kept trying to understand why it works for a long time and then finally when he understood he was so happy❤

  • @shivangawasthi8750
    @shivangawasthi8750 7 ปีที่แล้ว +9

    Hi Gaurav ,You nicely explained the explained the existance of solution for the equation but it can also be explained by the fact that if loop exist ,then when tortoise enter into the loop there are two particle in loop moving with different angular speed. So they have to meet at point.
    Relative angular speed is non zero constant so they must collide.

    • @gkcs
      @gkcs  7 ปีที่แล้ว +1

      +Shiwang Awasthi Ah, yes that is true. A really good and intuitive observation. Thanks!

  • @juliolopezmontalvo
    @juliolopezmontalvo 4 ปีที่แล้ว +5

    Finally someone explaining the math, thanks!

  • @sathwikabhignan1862
    @sathwikabhignan1862 2 หลายเดือนก่อน +1

    oh man, you are something else, fantastic explanation!!!

  • @laxminarayanchoudhary939
    @laxminarayanchoudhary939 3 ปีที่แล้ว +4

    So,this was a interesting questions. I found a solution after couple of years.😁
    At last excellent explaination.

  • @balajisv4052
    @balajisv4052 5 ปีที่แล้ว +5

    Your explanation deserves an extra like! Great video bro

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      Thanks!

  • @NohandleReqd
    @NohandleReqd 4 ปีที่แล้ว +3

    Gaurav maaaan! I saw this question a few years back And couldn't understand the solution. This video helped! 😄. Thanks

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

    Coming here after going through two different sources , now I don't need to go any where because solution
    make sense to me now :) . Thanks for your efforts!!

  • @vaibhavsingh1157
    @vaibhavsingh1157 6 ปีที่แล้ว +6

    Your explanation proves that from the meeting point i.e. c(j - 2i) it falls 'k' short to reach the intersection point, but it doesn't prove that from the meeting point it will take 'D' to reach the intersection point. I think what is missing over here is you will have to prove 'j-2i' is always equal to 1.

    • @nirbhaysingh5440
      @nirbhaysingh5440 4 ปีที่แล้ว

      I think if it is not equal to 1 then it will end up in a never-ending loop as they are moving at the same speed. Any thoughts? @Gaurav Sen

  • @pintunag
    @pintunag 5 ปีที่แล้ว +1

    start with fast and slow pointers.
    When slow meets fast, stop.
    Take a pointer p1, now assign head to p1.
    Move p1 by one position and slow by one position till p1 and slow meet.
    By the way, slow and p1 will meet exactly at the start of the loop.

  • @deepakchoudhary2970
    @deepakchoudhary2970 8 หลายเดือนก่อน

    Man this is wonderful, I was so stuck in this problem, went through multiple videos but finally found the perfect explanation. Thanks a ton, Gaurav.

    • @gkcs
      @gkcs  8 หลายเดือนก่อน +1

      You're welcome!

  • @sergemerto256
    @sergemerto256 ปีที่แล้ว

    No need to add maths equations too. Consider a very large cycle so that the first pointer when it reaches the starting node of the cycle is t distance behind the fast pointer. That means that fast pointer is (c-t) distance behind the first to catch up with it. So, we have (c-t) + y = 2y assuming at y nodes far away the both will meet. so y = (c-t) or in other words, the meeting point is at distance c from the head pointer. So now just take head and slow pointer(at the meeting point) and keep doing ->next until they both coincide. That will be the answer.

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

    atlast someone explained the algorithm with proof. amazing work.

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

    Was stuck at this problem over a month, now i finally understand it, thanks a lot!!!

  • @amitpurohit8816
    @amitpurohit8816 ปีที่แล้ว

    9:00 this was the missing link I was looking for!
    Thanks Gaurav bhai for this explanation!!!

  • @z08840
    @z08840 3 ปีที่แล้ว +2

    ...15 years ago it took me couple of hours to find this solution, though after some fiddling with analytical approach which was hard to interpret, finished with much easier graphical...

  • @SurajKumar-bw9oi
    @SurajKumar-bw9oi 4 ปีที่แล้ว +7

    Watch it twice to get the intuition :')

  • @inosuke44
    @inosuke44 5 ปีที่แล้ว +4

    after watching it 3 times finally got it, now i can also explain it someone

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

    Thanks a lot. This video help me understand the logic behind this.

  • @YashRaithatha1989
    @YashRaithatha1989 4 ปีที่แล้ว

    If space complexity is not an issue, we can iterate the loop till it reaches null and keep checking each node in HashSet. If a node doesn't exist then add it to HashSet and if it exists then break the loop and return that node which is the start of loop.
    To find length of the loop, we can keep extra Stack where we push the node while iterating until we discover the starting node of the loop as mentioned above. Then keep popping the nodes from the stack until we get the starting node and keep a counter while popping which is the length of the loop.

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

    The -K part really hits the point! Nice work!

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

    Beautiful Explanation Brother !!!

  • @vivekpathak3643
    @vivekpathak3643 ปีที่แล้ว

    thankyou for this much needed explanation.

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

    For those who are finding it little difficult to understand. Think of it this way:
    From the equation, it is already known that if you move the hare pointer(after meeting) d times, you will end up falling short behind k pointers and hare pointer is already at a k distance from the meeting point. So, falling down short a distance of k means that it will be at meeting point when it moves a distance d
    We don't know d. But we know the condition that hare pointer(after meeting) and head pointer will be equal only after moving d times. So, if they are equal we can conclude that they moved a distance d which is the meeting point

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

    Awesome Explanation !!

  • @shrishtychandra2003
    @shrishtychandra2003 5 ปีที่แล้ว +5

    Exactly the explanation I wanted to know.

    • @gkcs
      @gkcs  5 ปีที่แล้ว +1

      Thank you!

  • @adityatiwari1022
    @adityatiwari1022 4 ปีที่แล้ว +4

    The first question they ask in any damn interview.

  • @sachinbhandari345
    @sachinbhandari345 5 ปีที่แล้ว

    We can explain it even simpler if we set the starting of loop at the head node( the starting node). And then make it complex by adding the nodes at the starting point.

  • @mrnobodyy96
    @mrnobodyy96 ปีที่แล้ว

    Very good explanation. Thanks!

    • @gkcs
      @gkcs  ปีที่แล้ว

      Thank you!

  • @SS-sn4df
    @SS-sn4df ปีที่แล้ว

    Would you please consider having better lighting or using brighter Colors? It’s not a pleasant experience to look at the poorly lit whiteboard. It is so hard to read the text/diagrams written on the board. Sometimes it becomes so difficult that one would want to rather stop watching the video than going thru the struggle

  • @kanwarudaysingh
    @kanwarudaysingh 7 ปีที่แล้ว +1

    Hi Gaurav, Nice explanation. But you needed to say that k=(j-2i)x-D, so the slow pointer is at D iterations short of a cycle. So just by iterating it D number of time, we can get to the beginning

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

    4 years old vedio. Still clear explanation omong all vedios available in youtube.

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

    finally understood the logic thanks

  • @kollisashank1465
    @kollisashank1465 6 ปีที่แล้ว +2

    Hey Gaurav, let's say that if the question asked was to find out start point of the loop.
    How is this algorithm efficient than hashing?
    hashing would always take O(N) space and run time is O(N).
    but this algorithm runtime can be more than O(N).. but I agree that this will always take a constant space O(1).
    Just trying to understanding which one is better?

    • @gkcs
      @gkcs  6 ปีที่แล้ว +1

      This algorithm has a run time of O(N) actually. The loop will have both pointers touching inside within 2 rounds. Thats because the gcd of (loop length and 2) is atmost 2.
      This question is usually asked to test a candidate's resourcefulness. Hashing is the practical approach, but this works better in interviews 😊

    • @kollisashank1465
      @kollisashank1465 6 ปีที่แล้ว +1

      thanks Gaurav - This GCD info helps..

    • @sciphilo754
      @sciphilo754 6 ปีที่แล้ว

      Hey man, could you please explain the reasoning behind the GCD concept " The loop will have both pointers touching inside within 2 rounds. Thats because the gcd of (loop length and 2) is atmost 2". Or could you post a link where I can learn about it?

    • @gkcs
      @gkcs  6 ปีที่แล้ว

      It's not mentioned anywhere I think...here is another video which might help with the intuition: th-cam.com/video/D-DYtUmRMa4/w-d-xo.html

  • @uneq9589
    @uneq9589 5 ปีที่แล้ว +7

    Gotcha! I had to re-watch, but got it finally.

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      Awesome!

    • @uneq9589
      @uneq9589 5 ปีที่แล้ว

      @@gkcs Hi Gaurav, can you make videos on the problem www.interviewbit.com/problems/matrix-median/ and system design of a voting system?

    • @deeproy7292
      @deeproy7292 4 ปีที่แล้ว

      same here bro

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

    "loor" dekhe amio hese more gelam. ki khilli. Btw really great video! Keep it up. :)

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

    Loved this explanation, finally I fully understand how it works💡

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

    Great explanation, love the intuition you give !!!
    Amazing content, love your channel. I will share this video with my Junior college-mates :)

  • @ShivamSinghNIT
    @ShivamSinghNIT 5 ปีที่แล้ว +1

    Don't you think the distance k should represent distance from 2 to 4 instead of 3 to 4? Otherwise, the equation itself will not be right. Moreover, when you say, in the end, that we would be 'k' short that means from the meeting point we would go back to 3 not 2, correct?

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

    When ever we are choosing the slow pointer and fast pointer how fast they should move(can slow pointer move twice and fast pointer move three times .How we should decide.)

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

    If you're going to move D nodes in cycles you're going to fall short of K nodes->Starting point of cycle in LL.
    If you're going to move D nodes from head->Starting point of cycle in LL.

  • @DEBNATHKUNDU
    @DEBNATHKUNDU 4 ปีที่แล้ว

    I got a very weak solution to this problem and somehow passed the challenge. So, what I did was : I changed the data value of each visited node to a particular number (say -25 for my case) and then checked the data of the next node that my loop encounters. If that is equal to -25, I conclude that there is a loop, else not. So, I remembered which nodes I had visited. I know that no number can fit as a "sentinel" value, but yes I tried and got results. HOWEVER, THIS IS NOT A REAL SOLUTION, JUST A BYPASS!

  • @Stoic623
    @Stoic623 4 ปีที่แล้ว

    Hi Gaurav,
    Alternative code for finding the starting node of linked list cycle. Please let me know if there are any issues.
    if (loopExist) {

    slwPtr = slwPtr.next;
    List list = new ArrayList();
    while (slwPtr != fstPtr) {
    list.add(slwPtr);
    slwPtr = slwPtr.next;
    }
    slwPtr = head;
    while (slwPtr != fstPtr) {
    if (list.contains(slwPtr)) {
    return slwPtr;
    }
    slwPtr = slwPtr.next;
    }
    }
    start = slwPtr;
    return start;

    • @gkcs
      @gkcs  4 ปีที่แล้ว +1

      Nice try but no.
      No auxiliary space. That extra list used won't be allowed 🥺

  • @rakeshsinha9541
    @rakeshsinha9541 4 ปีที่แล้ว

    Thanks for the mathematical proof but test case which includes a whole circular linked list requires slight change in finding the start of the loop ie. head

  • @anmolmishra990
    @anmolmishra990 7 ปีที่แล้ว +1

    Such a nice explanation
    D+K part was explained so nicely, thanks bhaiya

  • @1096arvind
    @1096arvind 4 ปีที่แล้ว

    Keep up the stellar work! This is some really useful stuff!!

  • @shubhamchandra9258
    @shubhamchandra9258 6 ปีที่แล้ว

    Alternative soln. Insert a unique key(say $) as you traverse the list. i.e. 0 - $ - 1 - $ - 2 - $ - 3 - $ - 4 - $ - 5 - $ until you find the next link of a node to point to $ which will also mark the beginning of the cycle.

    • @gkcs
      @gkcs  6 ปีที่แล้ว +4

      Its a good idea if you can modify the list. Most of the interviewers don't allow that. :)

  • @mayukhchatterjee4863
    @mayukhchatterjee4863 4 ปีที่แล้ว

    Excellent Explanation. Keep up the good work man...!

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

    Great Explanation Sir

  • @sandyhan4829
    @sandyhan4829 4 ปีที่แล้ว +1

    I got it! Very clear. Thank you!

  • @jabbukka
    @jabbukka 5 ปีที่แล้ว +1

    Well explained! I didn't understand even though I read the solution but I totally got it with your video. Thank you :)

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      Glad to hear that 😁

  • @ivantishchenko4686
    @ivantishchenko4686 4 ปีที่แล้ว +1

    Great video. Thanks for sharing!

    • @gkcs
      @gkcs  4 ปีที่แล้ว

      Thanks 😊

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

    it could be explained much easily:
    1. the meeting point is at kth distance from loop entry(say 0th node)
    2. basically if we go back (-k) from meeting point(k) we will get to the loop entry 0th node (k+(-k) = 0)
    3. But the reality is that the meeting point k was met by doing 'c' cycles in loop which could be 1 cycle, 2 cycle.....c cycles
    4. so if the meeting point was achieved after 1 cycle (assume for simplification) then 1*n-k will give us the loop entry(0th node) which is at a distance 'D' from the head
    5 using the idea from step4, we move 'D' from meeting point(k) which will lead to loop entry(0th node)
    Note: if we move further +k from loop entry after doing step-4 then we would have made a complete cycle +1n and will be again at meeting point

    • @nvn6800
      @nvn6800 8 หลายเดือนก่อน

      thnx buddy reall yhelpful

  • @prateekjhabak5414
    @prateekjhabak5414 4 ปีที่แล้ว +1

    Nice explanation and logic deduction Gaurav_Sen..!!

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

    Thank you for this video

  • @narendra570
    @narendra570 7 ปีที่แล้ว +3

    U are really good at explaining things :)

    • @gkcs
      @gkcs  7 ปีที่แล้ว +2

      Thanks!

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

    when k = 0.. i.e loop is at the head.. you have to write extra code for this case.

  • @varunchopraapj
    @varunchopraapj 4 ปีที่แล้ว

    Hi Gaurav
    Thanks for the explanation.
    Food for thought: Will this work ...
    1. If Hare moves at 3x the speed and Tortoise moved 1x, for all the cases?
    2. If the Hare runs at 3x and Tortoise moved at 2x the rate?

    • @gkcs
      @gkcs  4 ปีที่แล้ว +2

      Food for thought: The gcd of the speeds must be 1 for a guaranteed meet :)

  • @bipul2138
    @bipul2138 6 ปีที่แล้ว +11

    FINALLY GOT IT! THANKS

  • @saurabhbhalla90
    @saurabhbhalla90 4 ปีที่แล้ว

    A little addition. The tortoise can never complete a cycle, so i will always be 0 in your equation

    • @gkcs
      @gkcs  4 ปีที่แล้ว

      It can. Try a few test cases (odd even cycle length)

    • @saurabhbhalla90
      @saurabhbhalla90 4 ปีที่แล้ว

      @@gkcs You might be assuming that at some point hare will jump over tortoise, which is not possible. The moment tortoise enters the cycle, hare would already be in the cycle and the distance between them will be less than a cycle. Since hare is faster, it'll catch the tortoise before it completes the cycle.
      Try coming up with an example where tortoise completes a cycle and you'll know what I'm saying.
      Unless I'm wrong. In that case your example would help me figure out why

    • @gkcs
      @gkcs  4 ปีที่แล้ว +1

      Looks correct. Thanks for pointing this out. I'll try some other examples and verify if this is true 😁👍

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

    great explanation. Written explanations out there are hard to understand. nice job!

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

    thanks for explanation 😃

  • @dev-skills
    @dev-skills 2 ปีที่แล้ว

    j < 2, will less than 2 tortoise can't go beyond two cycles in the loop without meeting hare.

  • @lallu12343
    @lallu12343 5 ปีที่แล้ว +1

    I would have known the solution posted by @shlok singh
    But your solution is smarter and shorter. I will keep this in mind.

  • @ChiCity511
    @ChiCity511 7 ปีที่แล้ว +1

    At around 3:45 why does n need the c and i variables? Doesn't the tortoise never make it through a full cycle since the hair would intersect it before that happens?

    • @gkcs
      @gkcs  7 ปีที่แล้ว

      +TrollkimNoah That might be true. But in the worst case, the tortoise and hare will meet at the start of the circle. Assume the whole graph to be a circular list.
      Then the tortoise makes 1 revolution and the hare makes 2.

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

    why can't we just mark the node when we visit them and when we reach them again that is the starting point of cycle. The complexity is same.

  • @AbhishekKumar-wx3rw
    @AbhishekKumar-wx3rw ปีที่แล้ว

    very nice explanation tank you

  • @sakshamsingh6351
    @sakshamsingh6351 4 ปีที่แล้ว

    watching it in 2020... deeply explained. thanks !

  • @DK-ox7ze
    @DK-ox7ze 2 ปีที่แล้ว

    Can you explain how to solve this question without using math in the interview? Because if we just writing these equations, the interviewer will think that we have mugged up the solution.

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

      Sure, just remember these two steps:
      1. Using a slow pointer and a fast pointer, find the "collision node"
      2. Observe that the head node of linked list and collision node are equidistant from where loop begins. So,
      move both pointers at 1x speed until they meet at loop start.
      I agree I think the math bit is overkill for interview setting.

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

    why do we only move the with difference of 1 step in slow and fast .?? why don't we move the slow pointer 1 step and fast pointer 3 or 4 steps ??

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

    Thanks a lot Gaurav Bhai

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

    is there any playlist of Interview Questions?

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

    Thank You Sir.

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

    Nice explanation

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

    How do you compare nodes in a cyclic graph though? In the final solution it seems like you need to start a pointer at the head (hptr) and at the meeting point (mptr), move forward by 1 and then check if hptr == mptr.
    how would the linked list implementation handle that? memory location or following the nodes?

  • @AnushaTripathi
    @AnushaTripathi 4 ปีที่แล้ว

    Awesome video! Thank you for explaining so clearly

  • @balamahalakshmi6909
    @balamahalakshmi6909 4 ปีที่แล้ว

    Why have you moved the D from 2nd node to 3rd node in the mid of video. It creates confusion

  • @jdelavega_
    @jdelavega_ 4 ปีที่แล้ว

    That was a great explanation. Thx so much for the help! :-)

  • @user-oy4kf5wr8l
    @user-oy4kf5wr8l 3 ปีที่แล้ว

    Thanks buddy!

  • @stalera
    @stalera 5 ปีที่แล้ว +3

    very well explained!

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      Thanks!

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

    nice explaination

  • @niharikaepuri3305
    @niharikaepuri3305 7 ปีที่แล้ว +1

    If we start the pointer at the start and one at the meeting point and move them by D+K then both end up at the same place? Can you be more specific around 8:05 to 8:12

    • @gkcs
      @gkcs  7 ปีที่แล้ว +1

      We end up at the same place, which is the meeting point, if we move the pointers from the start and meeting point respectively.
      The reason for this is D+K is where the meeting point is. Also, we see that D+K is a multiple of C, so any pointer inside the circle when moved by D+K nodes will revolve to the same place.
      Thats because moving a distance divisible by C is like taking a few cycles around the loop.

  • @mrfrog20110607
    @mrfrog20110607 6 ปีที่แล้ว +3

    Thank you so much! Your explanation is excellent! Really helps me a lot understanding the question!

    • @gkcs
      @gkcs  6 ปีที่แล้ว

      Thanks!

    • @akshaytata
      @akshaytata 5 ปีที่แล้ว

      Lovely explanation. Thank you

  • @sarthaksrivastav3408
    @sarthaksrivastav3408 4 ปีที่แล้ว

    Can we find the starting point of the loop by using map in which we store the addresses of visited nodes until we find a node whose address has already been in the map.

    • @gkcs
      @gkcs  4 ปีที่แล้ว +1

      No auxiliary space allowed.

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

    Please use a dark marker.

  • @abhishekkthakur1
    @abhishekkthakur1 6 ปีที่แล้ว +5

    Great Explaination! A dry run would really help though.

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

    Awesome!

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

    the equation that you figured out at 4:36 th sec that is N=C*(j-i). Does it mean that walking for some value of N
    (where n is D+K+C(i)) steps is same as making few complete cyclic loops ?

  • @reemasharma6188
    @reemasharma6188 4 ปีที่แล้ว

    Hi Gaurav, This was a great video. However, 1 thing which is still bugging me is , How every time ,Distance between starting node and the meeting point ends up to be equal to D i.e. if tortoise is moving D steps from head and hare is moving D steps from meeting point, they will definitely meet at the starting point ?

    • @gkcs
      @gkcs  4 ปีที่แล้ว

      Have a look at the equations again. The meeting point and starting point are at a distance of D from the start of the loop.

  • @mayukhchatterjee4863
    @mayukhchatterjee4863 4 ปีที่แล้ว

    I have a question. How are we going to compute the length D? Because if we dont we cannot stop the pointer P1.