Challenge - 5 Problems
Path Compression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of find operation with path compression
What is the output of the following TypeScript code that uses path compression in a Union Find structure?
DSA Typescript
class UnionFind { parent: number[]; constructor(size: number) { this.parent = Array.from({ length: size }, (_, i) => i); } find(x: number): number { if (this.parent[x] !== x) { this.parent[x] = this.find(this.parent[x]); } return this.parent[x]; } union(a: number, b: number): void { 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); uf.union(3, 4); console.log(uf.find(2)); console.log(uf.parent);
Attempts:
2 left
💡 Hint
Remember that path compression updates the parent of each node visited during find to the root.
✗ Incorrect
The find operation with path compression sets the parent of nodes 1 and 2 directly to 0, the root of their set. So uf.find(2) returns 0, and the parent array updates to [0, 0, 0, 3, 3].
🧠 Conceptual
intermediate1:30remaining
Effect of path compression on parent array
After performing union(0,1), union(1,2), and then calling find(2) with path compression, what will be the state of the parent array for a Union Find of size 4 initialized as [0,1,2,3]?
Attempts:
2 left
💡 Hint
Path compression sets all nodes on the find path directly to the root.
✗ Incorrect
Calling find(2) compresses the path so that nodes 1 and 2 point directly to 0, the root. So parent array becomes [0, 0, 0, 3].
🔧 Debug
advanced2:00remaining
Identify the error in path compression implementation
What error will the following TypeScript code produce when calling uf.find(2)?
DSA Typescript
class UnionFind { parent: number[]; constructor(size: number) { this.parent = Array.from({ length: size }, (_, i) => i); } find(x: number): number { if (this.parent[x] !== x) { this.find(this.parent[x]); } return this.parent[x]; } } const uf = new UnionFind(3); uf.parent = [0, 2, 1]; console.log(uf.find(2));
Attempts:
2 left
💡 Hint
Check if the recursive call updates the parent array.
✗ Incorrect
The find method calls itself recursively but does not update parent[x] with the result, causing infinite recursion when parent[x] != x.
❓ Predict Output
advanced2:30remaining
Parent array after multiple unions and finds with path compression
What is the parent array after executing the following code?
DSA Typescript
class UnionFind { parent: number[]; constructor(size: number) { this.parent = Array.from({ length: size }, (_, i) => i); } find(x: number): number { if (this.parent[x] !== x) { this.parent[x] = this.find(this.parent[x]); } return this.parent[x]; } union(a: number, b: number): void { 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); uf.find(4); console.log(uf.parent);
Attempts:
2 left
💡 Hint
The union operations connect all nodes into one set with root 0. Path compression updates parents.
✗ Incorrect
All nodes 0-5 become connected with root 0. The find(5) compresses paths so all parents point to 0.
🧠 Conceptual
expert1:30remaining
Time complexity impact of path compression in Union Find
Which statement best describes the effect of path compression on the time complexity of Union Find operations?
Attempts:
2 left
💡 Hint
Consider how path compression flattens the tree structure over time.
✗ Incorrect
Path compression flattens the tree, making future find operations very fast. The amortized time per operation becomes almost constant, bounded by the inverse Ackermann function.