The Redundant Connection problem gives you n nodes labeled 1..n and n edges forming a tree plus one extra edge (creating a cycle). Find the redundant edge — the last edge that, if removed, leaves a valid tree. This coding problem uses Union-Find to detect which edge creates the first cycle. The union find, BFS, graph, and DFS interview pattern is the core.
InMobi, Microsoft, Meta, Amazon, TikTok, Google, and Bloomberg ask this as the canonical Union-Find problem. It demonstrates cycle detection in an undirected graph — the first edge that connects two already-connected nodes is the redundant one.
Union-Find with cycle detection. Process edges in order. For each edge (u, v): if find(u) == find(v), they're already connected → this edge creates a cycle. Return it. Else, union(u, v) and continue.
edges=[(1,2),(1,3),(2,3)]. Process:
Redundant Connection is the standard Union-Find "cycle detection" problem. Process edges in order, union non-connected nodes, and the first edge creating a cycle is the answer. For undirected graphs, Union-Find is simpler than DFS for cycle detection. Practice both approaches: Union-Find (for undirected) and DFS with visited coloring (for directed). See also Redundant Connection II (directed graphs).
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Is Graph Bipartite? | Medium | Solve | |
| Graph Valid Tree | Medium | Solve | |
| Count the Number of Complete Components | Medium | Solve | |
| Possible Bipartition | Medium | Solve | |
| Number of Operations to Make Network Connected | Medium | Solve |