Find the Duplicate and Cycle Length in a Functional Graph
Linked ListsAuto-graded · 17 test casesLast updated
Tests cycle detection on an index-as-next-pointer array to find the single duplicate value and the length of the cycle it creates.
Functional Graph Cycle Detective
You are given a list nodes of n + 1 integers where every value is in the range [1, n] inclusive. Because there are n + 1 values but only n distinct labels, exactly one label appears twice.
Interpret the list as a functional graph: node at index i has a directed edge to node at index nodes[i]. Because one value is repeated, this graph contains exactly one cycle.
Your task is to return a tuple (duplicate, cycle_length) where:
duplicate— the integer label that appears exactly twice (this label is the entry point of the cycle).cycle_length— the number of nodes that form the cycle (i.e., the length of the smallest cycle reachable from index0).
How the traversal works
Starting from index 0, follow the chain:
index 0 → index nodes[0] → index nodes[nodes[0]] → …
Because exactly one value is duplicated, this chain will eventually revisit a node, forming a rho (ρ) shaped path: a tail leading into a cycle.
Constraints
2 ≤ len(nodes) ≤ 2 000len(nodes) == n + 1- Every element of
nodesis in[1, n]wheren = len(nodes) - 1 - Exactly one value in
nodesappears exactly twice; all others appear exactly once - The answer
(duplicate, cycle_length)is unique nodes[0]is never part of a duplicate (index0is always the tail start, not a cycle node itself) — the traversal from index0is guaranteed to reach the cycle
Examples
Example 1
Input: find_duplicate_and_cycle_length([2, 1, 2])
Output: [2, 1]
Example 2
Input: find_duplicate_and_cycle_length([3, 4, 1, 3, 2])
Output: [3, 1]
Example 3
Input: find_duplicate_and_cycle_length([2, 4, 3, 1, 2])
Output: [2, 4]
Hints
Hint 1
Think of each element nodes[i] as the 'next pointer' of node i — you can simulate a linked list walk starting at index 0 without building any explicit structure.
Hint 2
Floyd's tortoise-and-hare algorithm detects the meeting point inside the cycle; a second phase (reset one pointer to the start and advance both one step at a time) finds the exact entry point — that entry point index equals the duplicate value.
Hint 3
Once you know the entry point, measure the cycle length by advancing a single pointer around the loop and counting steps until it returns to the entry point.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is a classic application of Floyd's tortoise-and-hare cycle detection, applied to an index array interpreted as a linked list.
Key Insight
Treat nodes as a linked list where index i points to index nodes[i]. Because there are n+1 values all in [1, n], exactly one index is pointed to by two different predecessors — this is precisely the duplicate and the cycle's entry point.
Algorithm
Phase 1 — Find intersection inside the cycle.
Start slow = nodes[0] and fast = nodes[nodes[0]]. Advance slow by 1 step and fast by 2 steps until they meet. They are guaranteed to meet inside the cycle.
Phase 2 — Find the cycle entry point (= duplicate).
Reset slow = 0 (the logical start of the chain). Advance both slow and fast one step at a time. The index where they meet is the entry point of the cycle. This index equals the duplicate value, because it is the first node reachable from two different predecessors.
Phase 3 — Measure cycle length.
From the entry point, walk forward one step at a time, counting steps until returning to the entry point. That count is cycle_length.
Why It Is Correct
The pigeonhole principle guarantees exactly one duplicate. The functional graph built from values in [1, n] over n+1 nodes must contain a cycle, and the entry point of that cycle is the duplicated value (it is pointed to by both its "natural" predecessor in the chain and by the extra occurrence).
Floyd's algorithm correctly identifies this entry point in O(n) time and O(1) extra space.
Complexity
- Time: O(n) — each phase traverses at most O(n) steps.
- Space: O(1) — only a constant number of pointer variables are used.
Reference Solution
from typing import List
def find_duplicate_and_cycle_length(nodes: list) -> tuple:
# Phase 1: detect intersection point inside the cycle
slow = nodes[0]
fast = nodes[nodes[0]]
while slow != fast:
slow = nodes[slow]
fast = nodes[nodes[fast]]
# Phase 2: find cycle entry point (= the duplicate value)
slow = 0
while slow != fast:
slow = nodes[slow]
fast = nodes[fast]
duplicate = slow
# Phase 3: measure cycle length
cycle_length = 1
current = nodes[duplicate]
while current != duplicate:
current = nodes[current]
cycle_length += 1
return (duplicate, cycle_length)