Challenge - 5 Problems
Union Find Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of find operation with path compression
What is the output of the following Java code snippet that uses path compression in Union Find?
DSA Java
class UnionFind { int[] parent; public UnionFind(int n) { parent = new int[n]; for (int i = 0; i < n; i++) parent[i] = i; } public int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } public void union(int a, int b) { int rootA = find(a); int rootB = find(b); if (rootA != rootB) parent[rootB] = rootA; } } public class Main { public static void main(String[] args) { UnionFind uf = new UnionFind(5); uf.union(0, 1); uf.union(1, 2); System.out.println(uf.find(2)); System.out.println(java.util.Arrays.toString(uf.parent)); } }
Attempts:
2 left
💡 Hint
Remember that path compression updates the parent of all nodes on the find path to the root.
✗ Incorrect
After union(0,1) and union(1,2), the root of 0,1,2 is 0. Calling find(2) compresses the path, setting parent[2] = 0. So parent array becomes [0,0,0,3,4].
🧠 Conceptual
intermediateEffect of path compression on parent array
Given a Union Find structure with parent array [0, 0, 1, 3, 4], what will be the parent array after calling find(2) with path compression?
Attempts:
2 left
💡 Hint
Path compression sets the parent of the node directly to the root.
✗ Incorrect
Node 2's parent is 1, whose parent is 0. After find(2), parent[2] is updated to 0, so the array becomes [0, 0, 0, 3, 4].
🔧 Debug
advancedIdentify the bug in path compression implementation
What error will occur when running this Union Find find method with path compression?
DSA Java
public int find(int x) { if (parent[x] == x) { return x; } parent[x] = find(parent[x]); return parent[x]; }
Attempts:
2 left
💡 Hint
Check if all code paths return a value.
✗ Incorrect
The original method did not return a value after the recursive call, causing a compilation error for missing return statement. Adding 'return parent[x];' fixes the method.
❓ Predict Output
advancedOutput after multiple unions and finds with path compression
What is the output of the parent array after these operations?
DSA Java
UnionFind uf = new UnionFind(6); uf.union(0, 1); uf.union(1, 2); uf.union(3, 4); uf.union(4, 5); uf.union(2, 5); uf.find(5); System.out.println(java.util.Arrays.toString(uf.parent));
Attempts:
2 left
💡 Hint
Trace unions carefully and remember path compression updates parents on find.
✗ Incorrect
All nodes 0,1,2,3,4,5 become connected with root 0 after union(2,5). find(5) compresses path, setting parent of 3,4,5 to 0.
🧠 Conceptual
expertTime complexity impact of path compression in Union Find
What is the amortized time complexity of the find operation with path compression in a Union Find data structure?
Attempts:
2 left
💡 Hint
Path compression combined with union by rank gives almost constant time.
✗ Incorrect
The find operation with path compression has amortized time complexity O(α(n)), where α(n) is the inverse Ackermann function, which grows extremely slowly and is practically constant.
