0
0
DSA Cprogramming~20 mins

Path Compression in Union Find in DSA C - 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 code snippet that performs find operations with path compression on a Union Find structure?
DSA C
int parent[6] = {0, 0, 1, 2, 3, 4};

int find(int x) {
    if (parent[x] != x) {
        parent[x] = find(parent[x]);
    }
    return parent[x];
}

int main() {
    printf("%d\n", find(5));
    printf("%d\n", parent[5]);
    return 0;
}
A
5
5
B
4
4
C
0
0
D
1
0
Attempts:
2 left
💡 Hint
Trace the parent array and see how path compression updates parent[5].
🧠 Conceptual
intermediate
2:00remaining
Effect of path compression on parent array
Given a Union Find parent array: [0, 0, 1, 2, 3, 4], after calling find(5) with path compression, what will be the updated parent array?
A[0, 0, 1, 2, 3, 0]
B[0, 0, 0, 0, 0, 0]
C[0, 1, 2, 3, 4, 5]
D[0, 0, 1, 1, 3, 4]
Attempts:
2 left
💡 Hint
Path compression updates parents along the path to the root.
🔧 Debug
advanced
2:00remaining
Identify the bug in path compression implementation
What error will the following code cause when calling find(3)?
DSA C
int parent[4] = {0, 0, 1, 2};

int find(int x) {
    if (parent[x] == x) {
        return x;
    }
    return find(parent[x]);
}

int main() {
    printf("%d\n", find(3));
    return 0;
}
ACorrect output: 0
BRuntime error: segmentation fault
CCompilation error: missing return statement
DStack overflow due to infinite recursion
Attempts:
2 left
💡 Hint
Check if path compression is applied and if parent array changes.
Predict Output
advanced
2:00remaining
Output after union and find with path compression
What is the output of the following code snippet?
DSA C
int parent[5] = {0, 1, 2, 3, 4};

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

int find(int x) {
    if (parent[x] != x) {
        parent[x] = find(parent[x]);
    }
    return parent[x];
}

int main() {
    union_set(0, 1);
    union_set(1, 2);
    union_set(3, 4);
    printf("%d\n", find(2));
    printf("%d\n", parent[2]);
    printf("%d\n", find(4));
    return 0;
}
A
0
0
3
B
1
1
3
C
0
1
4
D
2
0
4
Attempts:
2 left
💡 Hint
Trace unions and path compression updates carefully.
🧠 Conceptual
expert
2:00remaining
Time complexity impact of path compression
Which statement best describes the time complexity impact of path compression in Union Find operations?
APath compression makes union operations slower but find operations faster.
BPath compression increases the worst-case time complexity to linear per find operation.
CPath compression has no effect on time complexity but reduces memory usage.
DPath compression reduces the amortized time per find operation to nearly constant, specifically inverse Ackermann function time.
Attempts:
2 left
💡 Hint
Consider how path compression flattens the tree structure over multiple operations.