Interview Question: Start of Loop in a Linked List

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys GBCs we have this question with us today which is open after the interviews that you have a big list with you and you have to retake if there is a group in it or not so the very famous efficient for this is having a hare and a tortoise with you so the hair as you can expect moves to nodes or iterations so this is a fast pointer and the second one which moves the tortoise moves one node per iteration okay so these two are moving at different speeds every iteration there's an advancement of the head by two and as authorized by one and both of them start as the starting node this is zero over here and you can see that they will meet at some hidden by the time so the hair will be pointy over here along the tour eyes what happens is after money relations the total is is pointing cure if the hair is over here after two iterations the total is finding where the head was the head is inside this loop and now it's pointing out here opportunity reasons you have the daughter is fighting hero in the hair it is pointing here and after four iterations the total is point to node 4 and the head just to and point along with the total as node 4 so if this condition ever happens it's the daughter is and the head are pointing to the same node that means that there is a do it logically it would be impossible for notice it has to be pointing at the same node if they're moving at different speeds the hair should have been allocated and so if they are pointing at the same node then never loop if head is pointing toward is is pointing then do report a loop otherwise the hair ever reaches the end of the list then you report no loop so this algorithm is pretty simple and it works for a meter you have the total is moving at this one iteration or one word per iteration so the total number of nodes that the author is moving is n after n iterations and the total number of moves note that the hair root is to end our presentation now you can see that the dollar is and the head could have been going around in circles in this in this hoop so this quantity is equal to let's say this part of the list this part which does not consist of the the loop is the inside okay in fact this part is the inside and the point at which they need each other is K so this part these two nodes these three nodes in pipe is equal to kiss that's the distance case so n would be equal to D plus k plus some number of cycles so the length of the cycle C into Amy but we don't know so we call that I so I you were made by is the first total and the hair is moving the same distance D plus it's method if it hasn't hasn't that distance of k plus C it has also made some loops along but it might have made more loops plus J first right so these two information tell us the amount by which the I just have been moving and now if you subtract these two equations so subtract this equation from this one then you get n equal to TN g castle of just C into J minus I and what does that tell you it tells you that you need to have an end that needs to exist an N because this is a growth using such that that is equal to C into J minus I and J minus I can be any teachers C is a constant so this equation needs to have solutions and you know that it's true because you can't use any arbitrary n such that this condition is true okay so our hypothesis of having a hair and her daughter is moving around and then finally meeting each other is true so this is a solution for find out if there is a loop or not the more complicated question actually is if there is a loop tell me what is the start of the loop okay so the spoiler so you have these two equations and there will be substituting the value of n using the first equation so that will be 2 into D plus 2 k plus 2 I into C this is equal to RHS d plus k plus J into see what your end up happening is if you take these bumps analogous your d plus k plus 2 I minus J it is C equal to 0 so that gives u d+ k equal to j minus 2i jesusí okay as we are o allah say middle question at moment Andy and I came up to this point but I could think ahead so you need a key inside okay what you need to realize is that T plus K is this is D and this distance K so I change the notation some little bit because this gives you the right answer so and if you think of it logically K is the extra distance I can move from the start of the of this U and D is the distance up to the loop so these are the right terms appear up D plus K is equal to j j- to I into C and changing these two terms is not going to change obviously lastork is there a possibility if you have a hair in a tortoise running at a meeting point those equation is visible in fact these two are the exact equations that we has then also so let's come back to the Reagan question see this K is equal to so on and so forth what does this mean if you have a pointer at the start I can use D plus K that means it moves P this distance Plus this distance so it should end up at the meeting point similarly if you start at a meeting point and you D plus K that is equal to main are in circles for some amount of time so this point can be ignored C is very important understand this is a multiple of C which is the length of the loop that means that you be moving around in use and ending up at the meeting point so indeed this case iterations you'll end up right here okay what is the tell us so if you start from the start or you start at the meeting point both cases if you move by Reese's case you end up as a meeting point what is what is the starting point of the loop so let's move just denotes D is equal to by this equation J minus 2i into C minus K okay so from the start if you move denotes you're going to end up here and from the meeting point if you move denotes you're going to end up after making C cycles so you're going to make sense except on the very last cycle you're going to show us fall short by keynotes this is very important they weighed upon short by keynotes what does that take you are going to fall short by kilos so you'll end up over here if they are able to trim your way to power out by two loads to the to the meeting point again so that will be over here and if you start argan oil you will end up or cure so if you have these two minus P 1 and P 2 and you move them one by one for a certain number of times the moment they meet each other which is this point is the start of the loop that's what this equal to means so take your time get a net around it if you move denotes that's the only time and these two points is a meeting that's the shortest pack by which these two point in the meeting and therefore we take one load as a start p1 move it to the right one by one and we take another point of p2 and move it around this loop one by one and it's been doing it up over here that's when we know that they have moved T nodes and that's when we know as we're actually these the Stossel you so this was an interesting question I find a common solution after a couple of users so it gives you a rate also questions please leave them in the comments below and be sharing one relevant link in the description below so until next time see you
Info
Channel: Gaurav Sen
Views: 110,642
Rating: 4.492228 out of 5
Keywords: Loop Detection, Linked List with Loop, Hare and Tortoise, Find Loop in a Linked List, Find Start of Loop in Linked List, Interview Question
Id: -YiQZi3mLq0
Channel Id: undefined
Length: 10min 36sec (636 seconds)
Published: Mon Apr 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.