Find the Entry Point of a Functional Rho-Shaped Sequence
Linked ListsAuto-graded · 15 test casesLast updated
Tests cycle detection on an index-based "next" array by finding where the tail of a rho-shaped sequence first enters its cycle.
Rho-Shaped Sequence Entry Point
A rho-shaped sequence (named after the Greek letter ρ) starts at index 0, follows a chain of "next" pointers, then eventually loops back to some earlier index — forming a tail leading into a cycle.
You are given a list nxt of length n where nxt[i] is the next index to visit after index i. Starting from index 0 and following the chain, the sequence is guaranteed to eventually repeat (i.e., it is rho-shaped: a straight tail that enters a cycle).
Your task is to return the index at which the sequence first enters the cycle — the first index that is visited twice when following the chain from index 0.
How the chain works
Beginning at index 0, you visit:
0 → nxt[0] → nxt[nxt[0]] → ...
The first index you reach that you have already visited before is the cycle entry point.
Constraints
2 ≤ n ≤ 20000 ≤ nxt[i] < nfor allinxtencodes exactly one rho-shaped sequence starting from index0(the sequence starting at0will always eventually cycle; the cycle entry point is always reachable from0)- There is exactly one correct answer.
Examples
Example 1
Input: find_cycle_entry([1, 2, 3, 1])
Output: 1
Example 2
Input: find_cycle_entry([1, 2, 0])
Output: 0
Example 3
Input: find_cycle_entry([1, 2, 3, 4, 2])
Output: 2
Hints
Hint 1
Try Floyd's tortoise-and-hare algorithm: move one pointer one step at a time and another two steps at a time until they meet inside the cycle.
Hint 2
Once the two pointers meet, reset one pointer to the start (index 0) and advance both one step at a time; their next meeting point is the cycle entry.
Hint 3
Alternatively, because n ≤ 2000 you can simply walk the chain while recording visited indices in a set, returning the first index you see for the second time.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
The list nxt defines a functional graph where each node has exactly one outgoing edge. Starting from index 0, the traversal must eventually revisit a node (since there are finitely many indices), forming a rho shape. We need the first revisited node — the cycle entry point.
Algorithm: Floyd's Cycle Detection (Tortoise and Hare)
Phase 1 — Detect a meeting point inside the cycle:
slowadvances one step at a time:slow = nxt[slow]fastadvances two steps at a time:fast = nxt[nxt[fast]]- Both start from
nxt[0](one step in, to avoid the trivial case where0itself is the entry but both pointers start equal). - When
slow == fast, both are somewhere inside the cycle.
Phase 2 — Find the cycle entry:
- Reset one pointer (
entry) to0. - Advance both
entryandslowone step at a time. - The step at which they meet is exactly the cycle entry point.
Why Phase 2 Works
If the tail length (distance from 0 to the cycle entry) is λ and the cycle length is μ, the meeting point in Phase 1 is at distance λ inside the cycle from the entry. Moving one pointer back to 0 and stepping both forward, they meet precisely at the cycle entry after λ more steps.
Correctness
- The algorithm is correct for any rho-shaped chain — even when the tail has length 0 (the cycle starts immediately at index
0). - It handles the edge case where
0is itself in the cycle: Phase 2 keepsentry = 0andslowinside the cycle; they converge at0.
Complexity
| Value | |
|---|---|
| Time | O(n) — both phases take at most O(n) steps |
| Space | O(1) — only a constant number of pointer variables |
Reference Solution
from typing import List
def find_cycle_entry(nxt: List[int]) -> int:
# Floyd's cycle detection
slow = nxt[0]
fast = nxt[nxt[0]]
# Phase 1: find meeting point inside the cycle
while slow != fast:
slow = nxt[slow]
fast = nxt[nxt[fast]]
# Phase 2: find cycle entry
# Reset one pointer to the start
entry = 0
while entry != slow:
entry = nxt[entry]
slow = nxt[slow]
return entry