DSA interview question
The scheduler bug from the previous problem needs fixing, not just detecting: the engineers want the exact node where the cycle begins, so they can cut the bad link. Floyd's algorithm has a remarkable second phase. After slow and fast meet inside the cycle, reset one pointer to the head and move both one step at a time; they meet again exactly at the cycle's entrance. A little algebra explains why, and interviewers usually ask for it.
Example: 3 -> 2 -> 0 -> -4, with -4 linking back to 2 → the node with value 2 (index 1).
Node* detectCycleStart(Node* head); // entrance node, or nullptr if there's no cycle
| Input (values, tail links to index) | Output (index of the entrance) | Explanation |
|---|---|---|
[3, 2, 0, -4], pos 1 | 1 | Value 2 |
[1, 2], pos 0 | 0 | The whole list is a cycle |
[1], pos -1 | -1 | No cycle (nullptr) |
[1, 2, 3, 4, 5, 6], pos 2 | 2 | Value 3 |
[1], pos 0 | 0 | Self-loop |
(Confirm: the output is reported as the entrance node's index for readability; the function itself returns the node or nullptr and must not modify the list.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in