KatInterview

Identify Cycles in Linear Chains and Directed Networks

Trees & GraphsAuto-graded · 22 test cases
MediumAuto-gradedTrees & Graphstrees-graphslinked-listsdfs

Last updated

Detect structural loops in both a next-pointer chain using constant space and a directed graph reachable from a given entry point.

You are tasked with detecting loops across two different pointer-based structures: a linear sequence (singly linked list) and a directed graph.

Both structures are provided in array form:

  1. Linear Chain:

    • linked_next: An array where linked_next[i] contains the zero-based index of the node following node i. A value of -1 indicates the end of the chain.
    • linked_head: The starting node index of the sequence. If linked_head is -1, the sequence is empty.
    • A loop exists in the chain if following the linked_next indices starting from linked_head leads back to a previously visited node.
  2. Directed Network:

    • graph: An adjacency list where graph[u] is a list of node indices reachable directly from node u.
    • graph_start: The starting node index for exploration.
    • A loop exists in the network if there is a directed path reachable from graph_start that visits a node more than once (i.e., a reachable directed cycle).

Write a function that returns a list of two boolean values [has_linear_cycle, has_graph_cycle].

Constraints

  • 0 <= len(linked_next) <= 10^5
  • -1 <= linked_next[i] < len(linked_next)
  • -1 <= linked_head < len(linked_next)
  • 0 <= len(graph) <= 10^5
  • Total edges across all lists in graph <= 2 * 10^5
  • 0 <= graph_start <= 10^5 (Note: graph_start may be greater than or equal to len(graph)).
  • Nodes in graph[u] are non-negative integers. If an edge references a node index >= len(graph), it should be treated as pointing to a node with no outgoing edges (or ignored safely upon out-of-bounds check).

Key Rules

  • For the linear chain, cycle detection must strictly operate in $O(1)$ auxiliary memory (ignoring the input arrays).
  • For the graph, only cycles reachable from graph_start should be reported as True. Unreachable cycles elsewhere in the graph should be ignored.
  • If graph_start is outside the range [0, len(graph) - 1], no nodes are reachable, so has_graph_cycle must be False.

Examples

Example 1

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

Output: [False, False]

Example 2

Input: detect_cycles([1, 2, 0], 0, [[1], [2], [0]], 0)

Output: [True, True]

Example 3

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

Output: [False, True]

Example 4

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

Output: [False, False]

Hints

Hint 1

For the linear chain, consider using two pointers moving at different speeds (Floyd's Tortoise and Hare algorithm) to maintain O(1) auxiliary space.

Hint 2

For the graph, standard DFS with three node states (unvisited, active/visiting, completed) can detect back-edges efficiently.

Hint 3

To avoid stack overflow errors on large linear graphs, implement the graph traversal iteratively using an explicit stack.