Magicsheet logo

Redundant Connection II

Hard
25%
Updated 8/1/2025

Redundant Connection II

What is this problem about?

The Redundant Connection II extends Part I to directed graphs (rooted trees). A valid rooted tree has one root with no incoming edges, all others with exactly one incoming edge, and no cycles. The extra edge creates either a node with two parents, a cycle, or both. Finding the correct redundant edge requires careful case analysis. The union find, BFS, graph, and DFS interview pattern is demonstrated at an advanced level.

Why is this asked in interviews?

Meta and Google ask this hard variant because it requires handling three cases: (1) a node has two incoming edges — one must be removed, (2) a cycle exists — the cycle-creating edge must be removed, (3) both issues combined. It tests the ability to handle multiple failure modes simultaneously.

Algorithmic pattern used

Three-case analysis + Union-Find. Find if any node has two parent edges (candidates). If yes, try removing each candidate and check if remaining graph is a valid tree (using Union-Find). If no such node, use Union-Find to find the edge creating the cycle.

Example explanation

edges=[(1,2),(2,3),(3,4),(4,1),(1,5)]. Node 1 has two parents: (4,1) and a hypothetical. Check candidates... Actually (4,1): 1 has incoming from 4 and no double-parent directly. But cycle: 1→2→3→4→1. Find cycle edge using Union-Find. Answer: (4,1) (last edge in cycle).

Common mistakes candidates make

  • Not handling all three cases separately.
  • Incorrectly identifying the "last" vs "first" candidate when two parents exist.
  • Not verifying the remaining graph after candidate removal.
  • Confusing directed Union-Find with undirected.

Interview preparation tip

Redundant Connection II requires careful case decomposition. Always check for double-parent nodes first — they provide candidate edges. If no double-parent, find the cycle-causing edge. When both occur together, the answer is always the second parent edge (the one creating the double-parent AND is part of the cycle). Practice directed graph validation: check in-degrees and cycle detection together.

Similar Questions