Find the Start of a Cycle in a Functional Graph
Linked ListsAuto-graded · 17 test casesLast updated
Tests cycle detection on index arrays by finding the entry node of a rho-shaped path in a functional graph encoded as a plain list.
Functional Graph Cycle Entry
A functional graph is a directed graph where every node has exactly one outgoing edge. Such a graph can be represented as a plain array next of length n, where next[i] is the index of the node that node i points to.
Starting from node 0, repeatedly following the next pointers traces a path that eventually enters a cycle (a rho-shaped path: a tail leading into a loop). Your task is to find the index of the first node that belongs to the cycle — i.e., the entry point where the tail meets the loop.
Formally, the cycle entry is the smallest-index node c such that:
cis reachable from node0by followingnextpointers, andclies on a cycle (there existsk ≥ 1withnext[next[...next[c]...]] = cafterksteps).
If two nodes could both serve as the entry point (which cannot happen in a true functional graph starting from 0), the tie-break is the smaller index. However, the graph is guaranteed to have exactly one cycle entry reachable from 0.
Task
Implement:
def find_cycle_entry(next_arr: list[int]) -> int:
Given next_arr, a list of n integers where next_arr[i] is the successor of node i, return the index of the cycle entry node reachable from node 0.
Important notes:
- Every node
isatisfies0 ≤ next_arr[i] < n, so no node is a dead end. - Node
0always eventually enters a cycle (the graph is finite). - The cycle entry is unique for the path starting at
0. - You must use O(1) extra space (pointer manipulation / Floyd's algorithm); do not use a visited set.
Constraints
2 ≤ n ≤ 20000 ≤ next_arr[i] < nfor allinext_arrdefines a valid functional graph (every node has exactly one outgoing edge)- The path from node
0always reaches a cycle - The cycle entry reachable from
0is unique
Examples
Example 1
Input: find_cycle_entry([1, 2, 3, 4, 2])
Output: 2
Example 2
Input: find_cycle_entry([1, 2, 0])
Output: 0
Example 3
Input: find_cycle_entry([1, 2, 3, 1])
Output: 1
Hints
Hint 1
Think of next_arr as a linked list where each node's 'pointer' is stored at that index. Floyd's tortoise-and-hare algorithm detects a meeting point inside the cycle — how do you then find the *entry* of the cycle?
Hint 2
After the fast and slow pointers meet inside the cycle, reset one pointer to the start (node 0) and advance both one step at a time; they will meet exactly at the cycle entry.
Hint 3
Be careful: the 'head' of this linked list is always node 0, not index 0 of the array in the traditional sense — the value next_arr[i] is the next node to visit, so start slow = 0, fast = 0 and take your first step before comparing.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is a direct application of Floyd's Tortoise-and-Hare cycle detection algorithm applied to an index array that acts as a linked list.
Why it works
Treat next_arr as a linked list where visiting node i means jumping to next_arr[i]. Starting from node 0, the sequence of visited nodes is:
0 → next_arr[0] → next_arr[next_arr[0]] → …
Because n is finite and every node has exactly one successor, the path must eventually revisit a node — forming a rho shape (tail + cycle).
Phase 1 — Detect a meeting point inside the cycle.
Run a slow pointer (one step at a time) and a fast pointer (two steps at a time), both starting at next_arr[0] (one step ahead of node 0). The classic analysis guarantees they will meet at some node inside the cycle after at most O(n) steps.
Phase 2 — Locate the cycle entry.
After the meeting point is found, reset one pointer to 0 (the start of the path) and leave the other at the meeting point. Advance both one step at a time. The meeting point of this second phase is exactly the cycle entry.
Proof sketch: Let μ be the tail length (distance from node 0 to the cycle entry) and λ be the cycle length. When the two pointers first meet, the slow pointer has traveled μ + a steps inside the cycle. It can be shown that a = λ - (μ mod λ), which means advancing another μ steps from the meeting point, or μ steps from 0, both land exactly on the cycle entry.
Correctness of the tie-break / uniqueness
A functional graph path starting at 0 has exactly one cycle entry — the first node re-visited. Floyd's algorithm finds it deterministically.
Complexity
| Time | O(n) — at most a constant number of full cycle traversals |
| Space | O(1) — only two integer pointers |
Reference Solution
def find_cycle_entry(next_arr: list[int]) -> int:
# Phase 1: detect a meeting point inside the cycle
slow = next_arr[0]
fast = next_arr[next_arr[0]]
while slow != fast:
slow = next_arr[slow]
fast = next_arr[next_arr[fast]]
# Phase 2: find the cycle entry
# Reset one pointer to the start; advance both one step at a time
entry = 0
while entry != slow:
entry = next_arr[entry]
slow = next_arr[slow]
return entry
Edge cases handled:
- The cycle starts immediately at node
0(node0points to itself or back into a cycle that includes0): Phase 2 will haveentry == slowimmediately at0, returning0correctly. - Very short arrays (
n = 2): still works since we always take at least one step before comparing in Phase 1. - Long tail with short cycle or short tail with long cycle: Floyd's algorithm handles both uniformly.