Skip to content
Decoder

Redundant Connection

Medium

Problem

A tree with n nodes labelled 1 to n had one extra edge added, so edges now has n undirected edges and exactly one cycle. Return an edge you can remove to turn the graph back into a tree. If several edges would work, return the one that appears last in edges.

Example 1

Input
edges = [[1, 2], [1, 3], [2, 3]]
Output
[2, 3]

Example 2

Input
edges = [[1, 2], [2, 3], [3, 4], [1, 4], [1, 5]]
Output
[1, 4]

Constraints

  • 3 <= n <= 1000
  • edges.length == n
  • 1 <= a < b <= n for every edge [a, b], and there are no repeated edges
  • The graph is connected

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.