0
0
Data Structures Theoryknowledge~20 mins

Disjoint set (Union-Find) in Data Structures Theory - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Union-Find Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the purpose of Disjoint Set

What is the primary purpose of a Disjoint Set (Union-Find) data structure?

ATo sort elements in ascending order using divide and conquer
BTo implement a priority queue with fast insertion and extraction
CTo store key-value pairs for fast lookup
DTo efficiently manage a collection of non-overlapping sets and quickly determine if two elements belong to the same set
Attempts:
2 left
💡 Hint

Think about grouping elements and checking their group membership efficiently.

📋 Factual
intermediate
2:00remaining
Time complexity of Union-Find operations

What is the amortized time complexity of the find and union operations in a Disjoint Set with path compression and union by rank/size?

AO(1) amortized
BO(log n) amortized
CO(α(n)) amortized, where α is the inverse Ackermann function
DO(n) amortized
Attempts:
2 left
💡 Hint

The inverse Ackermann function grows extremely slowly and is practically constant for all reasonable input sizes.

🔍 Analysis
advanced
2:00remaining
Result of union operations sequence

Given a Disjoint Set with elements {1, 2, 3, 4, 5} initially all separate, what is the number of distinct sets after performing these operations in order?

union(1, 2)
union(3, 4)
union(2, 3)
union(5, 1)
A2
B1
C3
D4
Attempts:
2 left
💡 Hint

Track how sets merge step-by-step.

Comparison
advanced
2:00remaining
Difference between union by rank and union by size

Which statement correctly describes the difference between union by rank and union by size in Disjoint Set implementations?

AUnion by rank attaches the tree with smaller height under the root of the tree with larger height, while union by size attaches the tree with fewer nodes under the root of the tree with more nodes
BUnion by rank attaches the tree with fewer nodes under the root of the tree with more nodes, while union by size attaches the tree with smaller height under the root of the tree with larger height
CUnion by rank always results in taller trees than union by size
DUnion by rank and union by size are identical and interchangeable without any difference
Attempts:
2 left
💡 Hint

Consider what 'rank' and 'size' represent in the context of trees.

Reasoning
expert
2:00remaining
Detecting cycle in an undirected graph using Disjoint Set

Consider an undirected graph with edges added one by one. How can a Disjoint Set be used to detect if adding a new edge creates a cycle?

ACheck if the two vertices of the new edge belong to the same set; if yes, adding the edge creates a cycle
BCheck if the two vertices of the new edge belong to different sets; if yes, adding the edge creates a cycle
CUse Disjoint Set to count the total number of edges and vertices to detect cycles
DAlways add the edge without any checks; cycles cannot be detected using Disjoint Set
Attempts:
2 left
💡 Hint

Think about what it means if two vertices are already connected before adding an edge.