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 with path compression
What is the output of the following code snippet that uses path compression in Union Find?
DSA Javascript
class UnionFind { constructor(size) { this.parent = Array.from({ length: size }, (_, i) => i); } find(x) { if (this.parent[x] !== x) { this.parent[x] = this.find(this.parent[x]); } return this.parent[x]; } union(a, b) { const rootA = this.find(a); const rootB = this.find(b); if (rootA !== rootB) { this.parent[rootB] = rootA; } } } const uf = new UnionFind(5); uf.union(0, 1); uf.union(1, 2); console.log(uf.find(2)); console.log(uf.parent);
Attempts:
2 left
💡 Hint
Remember that path compression updates the parent of nodes to the root during find.
✗ Incorrect
The find method compresses the path by setting the parent of each visited node directly to the root. After union(0,1) and union(1,2), all three nodes 0,1,2 share the same root 0. Calling find(2) returns 0 and updates parent[2] to 0. The parent array becomes [0,0,0,3,4].
🧠 Conceptual
intermediateEffect of path compression on tree height
Which statement best describes the effect of path compression in Union Find?
Attempts:
2 left
💡 Hint
Think about how path compression changes the parent links during find.
✗ Incorrect
Path compression makes every node on the path from a node to the root point directly to the root. This flattens the structure, making future find operations faster.
❓ Predict Output
advancedParent array after multiple unions and finds
What is the state of the parent array after the following operations with path compression?
DSA Javascript
class UnionFind { constructor(n) { this.parent = Array.from({ length: n }, (_, i) => i); } find(x) { if (this.parent[x] !== x) { this.parent[x] = this.find(this.parent[x]); } return this.parent[x]; } union(a, b) { const rootA = this.find(a); const rootB = this.find(b); if (rootA !== rootB) { this.parent[rootB] = rootA; } } } const 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); console.log(uf.parent);
Attempts:
2 left
💡 Hint
Trace the unions and remember that find compresses paths.
✗ Incorrect
After unions, nodes 0,1,2 form one set and nodes 3,4,5 another. Then union(2,5) connects these sets with root 0. The find(5) compresses the path, updating parents of 5 and 3 to 0. The parent array becomes [0,0,0,0,3,0].
🔧 Debug
advancedIdentify the bug in path compression implementation
What error will the following code cause when calling uf.find(2)?
DSA Javascript
class UnionFind { constructor(n) { this.parent = Array.from({ length: n }, (_, i) => i); } find(x) { if (this.parent[x] === x) { return x; } this.parent[x] = this.find(this.parent[x]); } union(a, b) { const rootA = this.find(a); const rootB = this.find(b); if (rootA !== rootB) { this.parent[rootB] = rootA; } } } const uf = new UnionFind(3); uf.union(0, 1); uf.union(1, 2); console.log(uf.find(2));
Attempts:
2 left
💡 Hint
Check if all code paths in find return a value.
✗ Incorrect
The find method returns x only if parent[x] === x. Otherwise, it updates parent[x] but does not return anything, so it returns undefined by default. Thus, uf.find(2) returns undefined.
🧠 Conceptual
expertAmortized time complexity with path compression
What is the amortized time complexity of find operations in Union Find with path compression and union by rank?
Attempts:
2 left
💡 Hint
The inverse Ackermann function grows extremely slowly.
✗ Incorrect
With path compression and union by rank, the amortized time per find operation is O(α(n)), where α(n) is the inverse Ackermann function, which is very slow growing and practically constant for all reasonable n.
