Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

684. Redundant Connection

Medium63.7% Acceptance
Depth-First SearchBreadth-First SearchUnion Find
Asked by:
Google
ProblemSolutions (12)VideosCompanies (2)Notes

Problem Statement

In this problem, a tree is an undirected graph that is connected and has no cycles.

You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.

Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.

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:

  • n == edges.length
  • 3 <= n <= 1000
  • edges[i].length == 2
  • 1 <= ai < bi <= edges.length
  • ai != bi
  • There are no repeated edges.
  • The given graph is connected.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
G
M
Microsoft

Approach

The key idea in #684 Redundant Connection is to detect the edge that creates a cycle in an undirected graph. Since the graph initially forms a tree and then receives one extra edge, that additional edge will connect two nodes that are already indirectly connected.

A common and efficient strategy is using the Union-Find (Disjoint Set Union) data structure. As you iterate through each edge, check whether the two nodes already belong to the same set using find(). If they do, adding this edge would form a cycle, making it the redundant connection. Otherwise, merge their sets using union().

An alternative approach uses DFS or BFS. Before adding an edge, perform a traversal to see if a path already exists between the two nodes. If a path exists, the current edge creates a cycle.

The Union-Find method is generally preferred because it processes edges efficiently with near-constant time operations, making it well suited for graph connectivity problems.

Complexity

ApproachTime ComplexitySpace Complexity
Union-Find (Disjoint Set)O(n α(n))O(n)
DFS / BFS Cycle CheckO(n^2) in worst caseO(n)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Approach 1: Union-Find Algorithm

This approach utilizes the Union-Find (or Disjoint Set Union, DSU) data structure to detect cycles. The key operations in Union-Find are 'find', which determines the representative of a set, and 'union', which merges two sets. When processing each edge, we attempt to unite the vertices. If both vertices are already in the same set, a cycle is detected, and this edge is the redundant one.

Time Complexity: O(n) where n is the number of edges because we process each edge once.
Space Complexity: O(n) due to the storage of the parent and rank arrays.

PythonC++JavaCJavaScriptC#
1class UnionFind:
2    def __init__(self, size):
3        self.root = list(range(size))
4        self.rank = [1


Explanation

This solution implements a Union-Find class with path compression and rank optimization to efficiently find and union nodes. By iteratively applying union operation on each edge, it detects the first edge causing a cycle, which is the redundant edge to be returned.

Approach 2: Depth-First Search (DFS)

The DFS approach involves simulating the construction of the graph and utilizing DFS to detect cycles whenever a new edge is being added. If a cycle is detected upon adjacent traversal, that edge is the redundant one.

Time Complexity: O(n^2), potentially examining each pair of nodes connected by added edges in the worst case.
Space Complexity: O(n), dictated by recursive stack depth in the DFS and the graph representation.

PythonC++JavaCJavaScriptC#
1

Video Solutions

Watch expert explanations and walkthroughs

Redundant Connection - Union Find - Leetcode 684 - Python

NeetCode
16:04123,096 views

Asked By Companies

2 companies
G
Google
M
Microsoft

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Same TreeEasy
Symmetric TreeEasy
Maximum Depth of Binary TreeEasy
Minimum Depth of Binary TreeEasy
More similar problems

Related Topics

Depth-First SearchBreadth-First SearchUnion FindGraph

Problem Stats

Acceptance Rate63.7%
DifficultyMedium
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Redundant Connection asked in FAANG interviews?

Yes, graph connectivity and Union-Find problems like Redundant Connection are commonly discussed in technical interviews at top companies. They test understanding of cycles, graph traversal, and efficient data structure design.

Can Redundant Connection be solved using DFS or BFS?

Yes, you can use DFS or BFS to check if a path already exists between two nodes before adding an edge. If a path exists, the edge would create a cycle, making it redundant. However, this approach is usually less efficient than Union-Find.

What is the optimal approach for Redundant Connection?

The optimal approach uses the Union-Find (Disjoint Set Union) data structure. It helps track connected components efficiently and detects when an edge connects two nodes already in the same set, indicating a cycle.

What data structure is best for solving Redundant Connection?

Union-Find is the most suitable data structure for this problem because it efficiently manages connectivity and cycle detection in graphs. It supports near constant-time union and find operations with path compression and rank optimization.

]
*
size
5
6
def
find
(
self
,
x
)
:
7
if
x
!=
self
.
root
[
x
]
:
8
self
.
root
[
x
]
=
self
.
find
(
self
.
root
[
x
]
)
9
return
self
.
root
[
x
]
10
11
def
union
(
self
,
x
,
y
)
:
12
rootX
=
self
.
find
(
x
)
13
rootY
=
self
.
find
(
y
)
14
if
rootX
!=
rootY
:
15
if
self
.
rank
[
rootX
]
>
self
.
rank
[
rootY
]
:
16
self
.
root
[
rootY
]
=
rootX
17
elif
self
.
rank
[
rootX
]
<
self
.
rank
[
rootY
]
:
18
self
.
root
[
rootX
]
=
rootY
19
else
:
20
self
.
root
[
rootY
]
=
rootX
21
self
.
rank
[
rootX
]
+=
1
22
return
True
23
return
False
24
25
class
Solution
:
26
def
findRedundantConnection
(
self
,
edges
)
:
27
uf
=
UnionFind
(
len
(
edges
)
+
1
)
# nodes are 1-indexed
28
for
u
,
v
in
edges
:
29
if
not
uf
.
union
(
u
,
v
)
:
30
return
[
u
,
v
]
def
dfs
(
graph
,
source
,
target
,
visited
)
:
2
if
source
==
target
:
3
return
True
4
visited
.
add
(
source
)
5
for
neighbor
in
graph
[
source
]
:
6
if
neighbor
not
in
visited
:
7
if
dfs
(
graph
,
neighbor
,
target
,
visited
)
:
8
return
True
9
return
False
10
11
class
Solution
:
12
def
findRedundantConnection
(
self
,
edges
)
:
13
graph
=
collections
.
defaultdict
(
set
)
14
for
u
,
v
in
edges
:
15
visited
=
set
(
)
16
if
u
in
graph
and
v
in
graph
and
dfs
(
graph
,
u
,
v
,
visited
)
:
17
return
[
u
,
v
]
18
graph
[
u
]
.
add
(
v
)
19
graph
[
v
]
.
add
(
u
)

Explanation

This solution constructs the graph incrementally while running DFS to check for paths from vertex u to vertex v before adding each edge. If the path exists, addition of the edge creates a cycle, thereby identifying it as redundant.