4 ms·
The Tortoise, the Hare, and the Cyclical Linked List
- xvedejas 5y agoThis is a cool little algorithm. I've heard about it first in the context of "nightmarish interview questions" though, where a programmer was meant to derive the technique from thin air :)
- petermcneeley 5y agoThough not as elegant and concise you can get a similar result by dropping a single marker at log number of steps.
- kerblang 5y agoYes it is routinely used as a way to humiliate candidates, because it's a perfect brain teaser: Devilishly simple but difficult to conjure unless you've seen it before - "Why couldn't I think of that???" Most undergrad CS curriculums won't cover it. https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoi...
- 908B64B197 5y ago> Most undergrad CS curriculums won't cover it. The serious ones do. At least, I found it to be commonly known.
- gkop 5y agoHeh, I recall that it’s in an early chapter of the original coding interview prep book.
- GuB-42 5y agoAnother one is: you have a list of numbers, more than half of them are the same number, and you have to find it. Ex: 1 2 3 1 1 3 1 => 1 Hints 1- It can be done in one pass, linear time, constant space 2- It doesn't have to be numbers 3- The algorithm it is literally just 2 statements in a loop
- comex 5y agoAre you allowed to modify the list? Edit: I think I figured it out!
- rowanG077 5y agoFeatures in this video: https://www.youtube.com/watch?v=pKO9UjSeLew https://www.youtube.com/watch?v=pKO9UjSeLew
- wahern 5y agoAccording to the solution in the video, the problem upthread was stated incorrectly. There's an additional constraint that only integers in the interval [1, N], where N is the size of the array, are permitted; or at least where the numbers can be trivially mapped to that interval (e.g. subtraction of an offset).
- Sharlin 5y agoObligatory link: Hexing the technical interview [1] [1] https://aphyr.com/posts/341-hexing-the-technical-interview https://aphyr.com/posts/341-hexing-the-technical-interview
- rbanffy 5y agoOh... This is wonderful.
- travisd 5y agoHaven't gone through the proof closely, but I suspect this would work for any prime for the tortoise, not just 2 (though 2 is probably optimal in actual implementation). This is the most I've used my discrete math degree in my 2.5 years post undergrad.
- contravariant 5y agoWell you want kx+b = x (mod m) to have a solution for all b and m (here b depends on how much earlier the hare enters the cycle). This means you'll need to have k-1 comprime to m for all m>0. Pretty sure k-1 = 1 is the only solution. As a counter example where both k and k-1 are prime consider k=3. Then: 3 x + b = x (mod 2) will fail to have a solution for odd b.
- thaumasiotes 5y ago> Well you want kx+b = x (mod m) to have a solution for all b and m I prefer to think about this another way. I'm also confused by your parent's comment "I suspect this would work for any prime for the tortoise, not just 2". In the post, 2 is the speed of the hare, and the tortoise's speed is 1. Anyway, what you want is for the hare to meet the tortoise. In the general case, tortoise and hare both have arbitrary speeds t and h and enter the cycle at random positions. The cycle is m nodes long. What you need is for (h - t), the amount by which the hare catches up to the tortoise, to be coprime to m. Since m could be anything, the only solution is h - t = 1. But you could have a hare going 6 nodes per timestep and a tortoise going 5.
- bombastry 5y agoIf you check every node that the hare enters, you don't need the relative speed to be coprime to the cycle-length. This eliminates the need for the hare and tortoise to finish a round on the same node; the hare passing the tortoise is sufficient evidence to detect a cycle. This change adds a performance penalty with the extra equality checks, but can reduce the total number of steps in certain contexts (e.g. lists with long cycles that appear at an early node can benefit from a much faster hare).
- e12e 5y ago> By now you may be wondering why we do not simply use a set, and return fast if we encounter a node already in the set. Or, indeed, to solve what we’re building towards, we could just return the first value which is already in the set. (...) > However, as I mentioned, this solution is spatially inefficient, Typically you'd need to store N pointers, and the list would be of size N(size_of(pointer) + size_of(node)) - which would normally be much larger than the pointers? (otherwise use an array, perhaps?) What are some real world cases where long linked list of very light nodes are best checked for cycles with this algorithm?
- thewakalix 5y agoPerhaps you don't have the entire linked list in memory, but are instead requesting individual nodes from a server.
- joppy 5y agoYou might have an abstract function f which takes and returns an integer, and you want to figure out if this function has “cycles”, eg if ffffff(n) = fff(n) etc. The numbers used might be enormous, and so trying to store them all is a non-starter.
- sischoel 5y agoIndeed, there are quite a few application in cryptography, where one has to search trough a huge space. Wikipedia actually listens a few: https://en.wikipedia.org/wiki/Cycle_detection#Applications https://en.wikipedia.org/wiki/Cycle_detection#Applications
- _0ffh 5y agoAs a semi off-topic aside, the title of this post is genius! It serves both the job of making those curious who are not in the know (and hopefully get their clicks), while also telling the rest exactly what it's about (and save our time).
- jsiaajdsdaa 5y ago"The reason is space complexity. You can't store every node in a set". Jokes on you, I've got 128 gigs of heap.