All practice tasks

Coding (Algo)

Find the First Repeated Link

Cycle detection · Medium

A linked-record inspector follows next pointers from one head. A corrupted chain may loop forever, so the inspector needs the first node that would be visited a second time. The meeting point of two moving probes is not necessarily that entry node.

Return the index where the reachable chain first enters its cycle using constant auxiliary space or null if the chain terminates.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ next: (integer | null)[], head: integer | null }

Fields

  • next is an array of next-node indexes or null; head is the starting index or null.
  • Every non-null pointer is in bounds. Disconnected nodes may exist, and reachable nodes may form a cycle.

Output

Return value

  • Return the first repeated node index, not its predecessor, a cycle length or an arbitrary node in the cycle.
  • A null head or an acyclic chain returns null. A self-loop returns its own index.

Constraints

  • 0 <= next.length <= 100000. Node identity is its index, not its stored value.
  • Do not mutate pointers or input arrays. Only nodes reachable from head contribute.

Examples

Example 1

{
  "head": 0,
  "next": [
    1,
    2,
    3,
    1
  ]
}

Returns

1

Example 2

{
  "head": 0,
  "next": [
    0
  ]
}

Returns

0

Required complexity

O(r) time and O(1) auxiliary space for r distinct reachable nodes

What this task practices

Find the First Repeated Link is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as medium difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.