Quick Overview

Find the first shared node of two acyclic linked chains using node identity and constant extra space. Practice the pointer-switching technique for unequal path lengths.

Find the Intersection of Two Linked Chains

Company: C3 AI

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

A singly linked structure is represented by an array `next_node`, where `next_node[i]` is the next node index or `-1` for the end. Two acyclic chains begin at `head_a` and `head_b`. Once the chains reach the same node index, they share the entire remaining suffix. Implement: ```text first_intersection( next_node: List[int], head_a: int, head_b: int ) -> int ``` Return the first node index shared by both chains, or `-1` if they do not intersect. ### Constraints - `0 <= len(next_node) <= 200_000` - Each head is `-1` or a valid node index. - Every nonnegative `next_node[i]` is a valid index. - The reachable chains are acyclic. - Target (O(a+b)) time and (O(1)) extra space, where (a) and (b) are chain lengths. ### Clarifications - Intersection means identical node index, not equal stored values. - Either head may be `-1`. - If both heads are the same valid index, return that index immediately. ```hint Equalize the traversed distance When a pointer reaches the end of one chain, redirect it to the other chain's head. Both pointers then traverse equal combined distances before meeting or reaching the end. ``` ### Examples ```text Input: next_node = [1, 2, -1, 2], head_a = 0, head_b = 3 Output: 2 Input: next_node = [1, -1, 3, -1], head_a = 0, head_b = 2 Output: -1 ``` ### Evaluation Focus - Node identity rather than value comparison. - Correct two-pointer termination for intersecting and disjoint chains. - Empty heads and unequal chain lengths. - Constant auxiliary space without marking visited nodes. ### Extension How would you detect and handle cycles if the acyclic guarantee were removed?

Overview: Find the first shared node of two acyclic linked chains using node identity and constant extra space. Practice the pointer-switching technique for unequal path lengths.

Read the full C3 AI Data Scientist interview experience this question came from

A shared next_node array represents acyclic singly linked chains: next_node[i] is the next node index after i, or -1 for the end. The two heads are each -1 or a valid index. Return the index of the first node shared by the chains from head_a and head_b, or -1 if they do not intersect.

Constraints

  • 0 <= next_node.length <= 200000
  • next_node[i] is -1 or a valid index in next_node
  • head_a and head_b are each -1 or valid indices
  • The chains reachable from both heads are acyclic

Examples

Input: ([1, 2, -1, 2], 0, 3)

Expected Output: 2

Explanation: The source example's chains first share node index 2.

Input: ([1, -1, 3, -1], 0, 2)

Expected Output: -1

Explanation: The source example's two chains are disjoint.

Hints

  1. Use one pointer per chain without storing visited nodes.
  2. When a pointer reaches -1, continue it from the other head so both pointers cover equal combined path lengths.

Loading coding console...

Show the approach

Approach

Walk one pointer from head_a and one from head_b. Whenever a pointer reaches -1, redirect it to the other chain's head. Thus pointer_a traverses chain A followed by chain B, while pointer_b traverses chain B followed by chain A. Both routes have the same total length, so any difference in the private-prefix lengths is canceled after the redirects. If the chains merge, the pointers then reach their first shared index on the same step: from that index onward the next links are identical, so an earlier equality would have been an earlier intersection. If they are disjoint, both pointers reach -1 after traversing both chains and the loop terminates there. Equal heads and empty chains terminate before any array access. Each chain is acyclic by contract, which guarantees bounded traversal.

Time complexity:
O(a + b), where a and b are the numbers of nodes reachable from the two heads.
Space complexity:
O(1) auxiliary space.