Back to Practice
#0296
Redundant Connection
MediumDSA28 min20 XP
Problem
Given undirected edges that form a tree plus one extra edge, return the extra edge that creates a cycle.
Why This Matters
This is union-find cycle detection in its simplest interview form.
Function Signature
def find_redundant_connection(edges):
Examples
Example 1
Inputedges = [[1,2],[1,3],[2,3]]
Output[2, 3]
1-2 and 1-3 already connect 2 and 3, so edge 2-3 creates a cycle.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Triangle
Input[[1,2],[1,3],[2,3]]
Expected[2, 3]
The third edge closes the cycle.
Later cycle
Input[[1,2],[2,3],[3,4],[1,4],[1,5]]
Expected[1, 4]
The edge 1-4 connects nodes already in the same component.
Hidden Test Categories
cycle at final edgelarger labelschain plus one closing edge