Qm
BrainteasersJane StreetCitadel

Is there a loop in the chain?

A sequence of boxes is linked: each box contains a pointer to a "next" box. Starting from the first box and following pointers, you either reach a box marked "end" or you go round forever, because some box points back to an earlier one.

You cannot mark boxes or remember which ones you have visited, and there may be millions of them. You may keep only a fixed small number of pointers into the chain.

How do you decide, in a bounded amount of time, whether the chain contains a loop? As a follow-up, how would you find where the loop begins?

Show a hint

Send two runners along the chain, one taking one step at a time and the other two. On a straight road the fast one simply finishes first. On a circular track, what happens?

Your answer

Solving needs a free account

Answers, streaks and solutions unlock when you are signed in. Reading the question and the hint stays free.

Discussion

Sign in to join the discussion · reading is open to everyone

💡 Discussion rules

  1. No full solutions here. Hints and approaches only.
  2. Complexity, edge cases and intuition are the point.
  3. Interview experiences are welcome. Respect your NDAs.

Loading discussion…

Learn the concepts

The theory behind this question.

Related questions

Finding one honest agent among liarsFind the duplicated numberFind the missing number in one passThe heaviest and the runner-up among eight coinsBoth the lightest and the heaviest of ten coins
All questions →