0
0
DSA Cprogramming~20 mins

Union Find Disjoint Set Data Structure in DSA C - 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 on a Union Find structure of size 5?
DSA C
int parent[5];
for (int i = 0; i < 5; i++) parent[i] = i;

void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) parent[b] = a;
}

int find_set(int v) {
    if (v == parent[v]) return v;
    return find_set(parent[v]);
}

union_sets(0, 1);
union_sets(1, 2);
union_sets(3, 4);

for (int i = 0; i < 5; i++) printf("%d ", parent[i]);
A[0, 0, 1, 3, 3]
B[0, 1, 2, 3, 4]
C[0, 0, 0, 3, 3]
D[1, 1, 1, 4, 4]
Attempts:
2 left
💡 Hint
Remember that union_sets attaches the root of one set to the root of another. The parent array shows direct parents, not necessarily roots after path compression.
🧠 Conceptual
intermediate
1:00remaining
Number of disjoint sets after unions
Given a Union Find structure initialized with 6 elements (0 to 5), after performing union(0,1), union(2,3), union(4,5), how many disjoint sets remain?
A1
B6
C2
D3
Attempts:
2 left
💡 Hint
Each union merges two sets into one, reducing the total count by one.
🔧 Debug
advanced
1:30remaining
Identify the bug in find_set with path compression
What error will the following find_set function cause when called repeatedly?
DSA C
int find_set(int v) {
    if (v == parent[v]) return v;
    return find_set(parent[v]);
    parent[v] = find_set(parent[v]);
}
AParent array never updates, so no path compression
BStack overflow due to infinite recursion
CNo error, works correctly
DCompilation error due to unreachable code
Attempts:
2 left
💡 Hint
Check the order of statements and unreachable code after return.
Predict Output
advanced
2:00remaining
Output of union-find with union by rank
What is the rank array after performing the following union operations on a union-find with union by rank?
DSA C
int parent[5], rank[5];
for (int i = 0; i < 5; i++) {
    parent[i] = i;
    rank[i] = 0;
}

int find_set(int v) {
    if (v == parent[v]) return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (rank[a] < rank[b]) parent[a] = b;
        else if (rank[b] < rank[a]) parent[b] = a;
        else {
            parent[b] = a;
            rank[a]++;
        }
    }
}

union_sets(0, 1);
union_sets(2, 3);
union_sets(1, 2);

for (int i = 0; i < 5; i++) printf("%d ", rank[i]);
A[1, 0, 0, 0, 0]
B[2, 0, 1, 0, 0]
C[1, 1, 0, 0, 0]
D[0, 0, 0, 0, 0]
Attempts:
2 left
💡 Hint
Rank increases only when two sets of equal rank are united.
🧠 Conceptual
expert
1:30remaining
Maximum number of unions before all elements connected
For a Union Find structure with n elements, what is the maximum number of union operations needed to connect all elements into a single set?
An - 1
Bn
Cn + 1
D2 * n
Attempts:
2 left
💡 Hint
Think about how many edges connect n nodes in a tree.