← Questions
Redundant connection: find the extra edge
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Apple's technical phone screen, last asked Apr 2026. It tests graphs and union-find.
A graph started as a tree on n nodes labeled 1 to n. Then one more edge was added between two different nodes, so the undirected graph now has exactly one cycle. Return the edge you can remove to make it a tree again. If several edges qualify, return the one that comes last in the input.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
find_redundant_edge(edges: list[list[int]]) -> list[int]—edges[i] = [u, v]is an undirected edge. Returns one edge from the input.
Example
find_redundant_edge([[1,2],[1,3],[2,3]]) -> [2,3]
find_redundant_edge([[1,4],[1,2],[2,3],[3,4]]) -> [3,4] # all four are on the cycle; take the last
find_redundant_edge([[1,2],[2,3],[3,4],[1,4],[1,5]]) -> [1,4] # [1,5] is not on the cycle
Constraints
-
n == len(edges)and3 <= n <= 1000. -
1 <= u, v <= nandu != v. - The graph is connected and has exactly one cycle.
Clarifying questions
- Can the same edge appear twice?
- Is the graph always connected, or could there be a separate component?
- What should happen if the input has no cycle at all?
What a strong answer covers
- Union-find over the edges in input order: the first edge whose endpoints are already connected closes the cycle, and it is also the last cycle edge in the input. Explain why that holds.
- Path compression and union by rank or size, for near-linear time.
- The brute force (remove each edge from the end and check the rest is a tree) and why it is
O(n²). - Tests: the smallest triangle, a cycle that uses every edge, and edges that hang off the cycle after it closes.
Follow-ups
- The edges are directed and the original graph was a rooted tree, so a node may now have two parents.
- Return every edge on the cycle.
- Edges arrive as a stream: report the first one that creates a cycle.
Where it's asked
- Apple · Technical phone screen · Last asked Apr 2026
What it tests
graphs · union-find
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
