Challenge - 5 Problems
Union Find Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Union Find after union operations
What is the printed parent array after performing the union operations shown below?
DSA Python
class UnionFind: def __init__(self, size): self.parent = list(range(size)) self.rank = [0] * size def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): rootX = self.find(x) rootY = self.find(y) if rootX != rootY: if self.rank[rootX] > self.rank[rootY]: self.parent[rootY] = rootX elif self.rank[rootX] < self.rank[rootY]: self.parent[rootX] = rootY else: self.parent[rootY] = rootX self.rank[rootX] += 1 uf = UnionFind(5) uf.union(0, 1) uf.union(1, 2) uf.union(3, 4) print(uf.parent)
Attempts:
2 left
💡 Hint
Remember that union by rank attaches the smaller tree under the root of the larger tree.
✗ Incorrect
After union(0,1) and union(1,2), nodes 0,1,2 share the same root 0. After union(3,4), nodes 3 and 4 share root 3. So parent array is [0,0,0,3,3].
🧠 Conceptual
intermediateNumber of connected components after unions
Given a Union Find structure initialized with 6 elements (0 to 5), after performing union(0,1), union(2,3), union(4,5), and union(1,2), how many connected components remain?
Attempts:
2 left
💡 Hint
Each union merges two components into one.
✗ Incorrect
Initially 6 components. union(0,1) merges 0 and 1 → 5 components. union(2,3) merges 2 and 3 → 4 components. union(4,5) merges 4 and 5 → 3 components. union(1,2) merges sets containing 0,1 and 2,3 → 2 components remain.
🔧 Debug
advancedIdentify the error in this Union Find implementation
What error will this code produce when calling uf.union(0, 1)?
DSA Python
class UnionFind: def __init__(self, size): self.parent = list(range(size)) self.rank = [0] * size def find(self, x): while self.parent[x] != x: x = self.parent[x] return x def union(self, x, y): rootX = self.find(x) rootY = self.find(y) if rootX != rootY: if self.rank[rootX] > self.rank[rootY]: self.parent[rootY] = rootX elif self.rank[rootX] < self.rank[rootY]: self.parent[rootX] = rootY else: self.parent[rootY] = rootX self.rank[rootX] += 1 uf = UnionFind(3) uf.union(0, 1)
Attempts:
2 left
💡 Hint
Check how find method updates parents during traversal.
✗ Incorrect
The find method uses a loop but does not update parent pointers to root, so path compression is missing. This is a logical inefficiency, not a runtime error.
📝 Syntax
advancedIdentify the syntax error in this Union Find code snippet
Which option correctly identifies the syntax error in the code below?
DSA Python
class UnionFind: def __init__(self, size): self.parent = list(range(size)) self.rank = [0] * size def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x]
Attempts:
2 left
💡 Hint
Look carefully at the if statement syntax.
✗ Incorrect
The if statement in find method lacks a colon at the end, causing a SyntaxError.
🚀 Application
expertUsing Union Find to detect cycle in an undirected graph
Given edges of an undirected graph: [(0,1), (1,2), (2,3), (3,0)], which union find operation detects a cycle when processing these edges in order?
DSA Python
class UnionFind: def __init__(self, size): self.parent = list(range(size)) self.rank = [0] * size def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): rootX = self.find(x) rootY = self.find(y) if rootX == rootY: return False # cycle detected if self.rank[rootX] > self.rank[rootY]: self.parent[rootY] = rootX elif self.rank[rootX] < self.rank[rootY]: self.parent[rootX] = rootY else: self.parent[rootY] = rootX self.rank[rootX] += 1 return True uf = UnionFind(4) edges = [(0,1), (1,2), (2,3), (3,0)] for i, (u,v) in enumerate(edges): if not uf.union(u,v): print(i)
Attempts:
2 left
💡 Hint
A cycle is detected when union returns False because both nodes share the same root.
✗ Incorrect
Edges 0,1,2 connect nodes without cycle. Edge (3,0) connects nodes already connected, so union returns False and prints index 3.
