0
0
DSA Typescriptprogramming~20 mins

Union Find Disjoint Set Data Structure in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Union Find Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Union Find after union operations
What is the printed parent array after performing the union operations shown below?
DSA Typescript
class UnionFind {
  parent: number[];
  constructor(size: number) {
    this.parent = Array(size).fill(-1);
  }
  find(x: number): number {
    if (this.parent[x] < 0) return x;
    this.parent[x] = this.find(this.parent[x]);
    return this.parent[x];
  }
  union(a: number, b: number): void {
    let rootA = this.find(a);
    let rootB = this.find(b);
    if (rootA !== rootB) {
      if (this.parent[rootA] <= this.parent[rootB]) {
        this.parent[rootA] += this.parent[rootB];
        this.parent[rootB] = rootA;
      } else {
        this.parent[rootB] += this.parent[rootA];
        this.parent[rootA] = rootB;
      }
    }
  }
}

const uf = new UnionFind(5);
uf.union(0, 1);
uf.union(1, 2);
uf.union(3, 4);
console.log(uf.parent);
A[-3, 0, 1, -2, 3]
B[-3, 0, 0, -2, 3]
C[-2, 0, 0, -3, 3]
D[-3, 1, 1, -2, 3]
Attempts:
2 left
💡 Hint
Remember that the parent array stores negative sizes for roots and points to root for others.
🧠 Conceptual
intermediate
1:30remaining
Number of disjoint sets after unions
Given the following union operations on a Union Find of size 6, how many disjoint sets remain?
DSA Typescript
const uf = new UnionFind(6);
uf.union(0, 1);
uf.union(2, 3);
uf.union(4, 5);
uf.union(1, 2);
A4
B3
C1
D2
Attempts:
2 left
💡 Hint
Count how many unique roots remain after unions.
🔧 Debug
advanced
2:00remaining
Identify the error in this Union Find implementation
What error will this code produce when calling uf.union(0, 1)?
DSA Typescript
class UnionFind {
  parent: number[];
  constructor(size: number) {
    this.parent = Array(size).fill(-1);
  }
  find(x: number): number {
    if (this.parent[x] < 0) return x;
    return this.find(this.parent[x]);
  }
  union(a: number, b: number): void {
    let rootA = this.find(a);
    let rootB = this.find(b);
    if (rootA !== rootB) {
      if (this.parent[rootA] < this.parent[rootB]) {
        this.parent[rootA] += this.parent[rootB];
        this.parent[rootB] = rootA;
      } else {
        this.parent[rootB] += this.parent[rootA];
        this.parent[rootA] = rootB;
      }
    }
  }
}

const uf = new UnionFind(2);
uf.union(0, 1);
AMaximum call stack size exceeded (infinite recursion)
BTypeError: Cannot read property of undefined
CNo error, runs correctly
DReferenceError: uf is not defined
Attempts:
2 left
💡 Hint
Check if find method updates parent during path compression.
Predict Output
advanced
2:00remaining
Output of find after unions and path compression
What is the output of the console.log statements after these operations?
DSA Typescript
class UnionFind {
  parent: number[];
  constructor(size: number) {
    this.parent = Array(size).fill(-1);
  }
  find(x: number): number {
    if (this.parent[x] < 0) return x;
    this.parent[x] = this.find(this.parent[x]);
    return this.parent[x];
  }
  union(a: number, b: number): void {
    let rootA = this.find(a);
    let rootB = this.find(b);
    if (rootA !== rootB) {
      if (this.parent[rootA] <= this.parent[rootB]) {
        this.parent[rootA] += this.parent[rootB];
        this.parent[rootB] = rootA;
      } else {
        this.parent[rootB] += this.parent[rootA];
        this.parent[rootA] = rootB;
      }
    }
  }
}

const uf = new UnionFind(4);
uf.union(0, 1);
uf.union(2, 3);
console.log(uf.find(1));
console.log(uf.find(3));
console.log(uf.parent);
A
0
2
[-2, 0, -2, 2]
B
1
3
[-2, 0, -2, 2]
C
0
3
[-2, 0, -2, 2]
D
0
2
[-2, 1, -2, 2]
Attempts:
2 left
💡 Hint
Remember find returns root and compresses paths.
🧠 Conceptual
expert
1:30remaining
Effect of union by size on tree height
Which statement best describes the effect of union by size in a Union Find data structure?
AIt does not affect tree height or efficiency; it only tracks the number of unions performed.
BIt always attaches the second argument's root under the first argument's root regardless of size.
CIt keeps the tree height minimal by always attaching smaller trees under larger ones, improving find efficiency.
DIt increases tree height by attaching larger trees under smaller ones, slowing down find operations.
Attempts:
2 left
💡 Hint
Think about how attaching smaller trees under larger roots affects depth.