Detect Functional-Graph Cycle Start on an Index Array
Linked ListsAuto-graded · 17 test casesLast updated
Tests cycle detection (Floyd's or similar) on an implicit linked list encoded as a next-pointer array, requiring the candidate to find where the cycle begins.
Functional Graph Cycle Start
You are given a list next_node of length n. Each element next_node[i] is an integer in [0, n-1], meaning "from node i, follow the pointer to node next_node[i]."
Starting from index 0, repeatedly follow pointers:
0 → next_node[0] → next_node[next_node[0]] → …
Because n is finite and every node has exactly one outgoing edge, this path must eventually revisit a node — forming a rho (ρ) shape: a (possibly empty) tail leading into a cycle.
Your task is to return the index of the node where the cycle begins — i.e., the first node that is visited twice when following the chain from index 0.
How the cycle entry is defined
Walk the chain from index 0. The cycle-entry node is the first index that appears more than once in that infinite walk. Equivalently, it is the node v such that some earlier position in the walk also landed on v, and no node before v in the walk satisfies that property.
Constraints
1 ≤ n ≤ 20000 ≤ next_node[i] ≤ n - 1for alli- The chain starting from index
0is guaranteed to eventually cycle (always true for a finite functional graph). - If
next_node[0] == 0, then index0is a self-loop and is its own cycle entry.
Examples
Example 1
Input: find_cycle_start([1, 2, 3, 4, 2])
Output: 2
Example 2
Input: find_cycle_start([1, 2, 0])
Output: 0
Example 3
Input: find_cycle_start([0])
Output: 0
Hints
Hint 1
Try keeping a visited set as you walk the chain; the first repeated index is the answer — but can you do it in O(1) extra space?
Hint 2
Floyd's tortoise-and-hare algorithm detects a cycle in O(n) time and O(1) space; after the two pointers meet inside the cycle, reset one pointer to the start and advance both one step at a time to find the entry.
Hint 3
When both pointers move at speed 1 after the meeting step, the step at which they coincide gives you the cycle-entry node directly.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is the classic cycle detection in a functional graph (linked-list cycle), solved with Floyd's Tortoise-and-Hare algorithm.
Why it works
Phase 1 – Detect the cycle.
Move tortoise one step and hare two steps per iteration, both starting from index 0. Because the sequence must cycle, the hare will eventually lap the tortoise and they will meet at some node inside the cycle.
Phase 2 – Find the entry.
Let:
µ= distance from node0to the cycle entryλ= cycle length
When the two pointers meet in Phase 1 they are at distance µ from the cycle entry (a classical result: the meeting point is exactly µ steps before the entry within the cycle). Resetting the tortoise to 0 and advancing both pointers one step at a time means after µ more steps the tortoise arrives at the entry for the first time, and the hare (which was µ steps behind the entry inside the cycle) also arrives at the entry — they coincide exactly at the cycle-entry node.
Correctness on edge cases
- Self-loop at 0 (
next_node[0] == 0): Phase 1 starts withtortoise = hare = 0immediately, but actuallytortoise = next_node[0] = 0andhare = next_node[next_node[0]] = 0— they are equal at the start, so the while loop is skipped. Phase 2 resets tortoise to0; hare is also0; the while loop is skipped again; we return0. Correct. - Cycle involving node 0 (e.g.,
[1,2,0]): the algorithm finds0as the entry. - Long tail, short cycle and short tail, long cycle: both handled uniformly.
Complexity
- Time: O(n) — Phase 1 takes at most O(µ + λ) steps, Phase 2 takes O(µ) steps. Since µ + λ ≤ n, overall O(n).
- Space: O(1) — only a constant number of pointer variables.
Alternative (simpler, O(n) space)
Walk the chain keeping a seen set; return the first repeated node. This is O(n) time and O(n) space and is equally acceptable in an interview if O(1) space is not required.
from typing import List
def find_cycle_start(next_node: List[int]) -> int:
# Phase 1: detect that a cycle exists using Floyd's algorithm.
# Tortoise moves 1 step, hare moves 2 steps.
tortoise = next_node[0]
hare = next_node[next_node[0]]
while tortoise != hare:
tortoise = next_node[tortoise]
hare = next_node[next_node[hare]]
# Phase 2: find cycle entry.
# Reset tortoise to start; keep hare at meeting point.
# Both advance 1 step at a time; they meet at the cycle entry.
tortoise = 0
while tortoise != hare:
tortoise = next_node[tortoise]
hare = next_node[hare]
return tortoise