Union-Find is the clean solution here and I knew that going in, but I second-guessed myself halfway through and started rambling about DFS instead.
Use Union-Find to process edges in order, and when an edge connects two already-connected nodes, it is part of the cycle. To handle multiple valid answers, process edges in reverse order and return the first edge that creates a cycle, which corresponds to the last such edge in the original input.
Pro tip: Clarify the tie-breaking rule upfront: if multiple edges can be removed, return the one that appears last in the input. This shows attention to detail and avoids ambiguity.
Recognize that the graph is a tree plus one extra edge, so it contains exactly one cycle. Removing any edge on that cycle restores a tree, but we need the one that appears last in the input.
Use Union-Find (Disjoint Set Union) to efficiently detect cycles while processing edges. This is optimal for connectivity checks.
Iterate through the edge list from last to first. For each edge, if its endpoints are already in the same set, it is the redundant edge; return it immediately.
If the endpoints are not connected, union their sets. Continue until the redundant edge is found.
The first redundant edge encountered in reverse order is the last redundant edge in the original order, satisfying the tie-breaking rule.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.