Redundant Connection
Signals to notice
Brute force first
For each edge, remove it and check if the remaining graph is still a tree. Rebuilds the graph for every edge. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.
The key insight
Union-Find: process edges one by one. If union(u,v) finds they're already connected, this edge creates a cycle — it's the redundant edge. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
What must stay true
In a tree with n nodes, there are exactly n-1 edges. The graph has n edges, so exactly one is redundant. That edge connects two nodes already in the same component — detected by Union-Find. As long as that statement keeps holding, you can trust the steps built on top of it.
Easy way to go wrong
Returning the first cycle edge found — the problem asks for the LAST such edge in the input order. Process all edges; the last one that causes a union to fail is the answer. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.