0
0
DSA Typescriptprogramming~20 mins

Path Compression in Union Find in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Path Compression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A
2
[0, 1, 2, 3, 4]
B
0
[0, 0, 0, 3, 3]
C
1
[0, 0, 1, 3, 3]
D
3
[0, 1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Remember that path compression updates the parent of each node visited during find to the root.
🧠 Conceptual
intermediate
1: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]?
A[0, 0, 0, 3]
B[0, 1, 0, 3]
C[0, 1, 2, 3]
D[1, 1, 1, 3]
Attempts:
2 left
💡 Hint
Path compression sets all nodes on the find path directly to the root.
🔧 Debug
advanced
2: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));
AReturns 0 without error
BTypeError: Cannot read property of undefined
CStack overflow due to infinite recursion
DReturns 2 without error
Attempts:
2 left
💡 Hint
Check if the recursive call updates the parent array.
Predict Output
advanced
2: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);
A[0, 0, 0, 0, 0, 0]
B[0, 0, 0, 0, 3, 3]
C[0, 1, 2, 3, 4, 5]
D[0, 0, 0, 3, 3, 0]
Attempts:
2 left
💡 Hint
The union operations connect all nodes into one set with root 0. Path compression updates parents.
🧠 Conceptual
expert
1: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?
APath compression changes the space complexity of Union Find from linear to logarithmic.
BPath compression increases the worst-case time complexity of union operations to linear time.
CPath compression has no effect on the time complexity of find or union operations.
DPath compression reduces the amortized time complexity of find operations to nearly constant, specifically inverse Ackermann function time.
Attempts:
2 left
💡 Hint
Consider how path compression flattens the tree structure over time.