Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of path compression in the Union Find data structure?
easy
A. To initialize the parent array
B. To merge two sets by size
C. To count the number of connected components
D. To speed up the find operation by making nodes point directly to the root

Solution

  1. Step 1: Understand the find operation

    The find operation locates the root parent of a node in Union Find.
  2. Step 2: Role of path compression

    Path compression makes all nodes on the path point directly to the root, speeding up future find calls.
  3. Final Answer:

    To speed up the find operation by making nodes point directly to the root -> Option D
  4. Quick Check:

    Path compression optimizes find = C [OK]
Hint: Path compression flattens tree for faster find [OK]
Common Mistakes:
  • Confusing path compression with union by size
  • Thinking it initializes the structure
  • Believing it counts components
2. Which of the following is the correct syntax for the find function with path compression in C?
easy
A. int find(int x) { while(parent[x] != x) x = parent[x]; return x; }
B. int find(int x) { if(parent[x] != x) parent[x] = find(parent[x]); return parent[x]; }
C. int find(int x) { if(parent[x] == x) return x; else return find(parent[x]); }
D. int find(int x) { parent[x] = x; return x; }

Solution

  1. Step 1: Identify path compression in code

    Path compression updates parent[x] during recursion to root.
  2. Step 2: Check each option

    int find(int x) { if(parent[x] != x) parent[x] = find(parent[x]); return parent[x]; } updates parent[x] with recursive find result, implementing path compression correctly.
  3. Final Answer:

    int find(int x) { if(parent[x] != x) parent[x] = find(parent[x]); return parent[x]; } -> Option B
  4. Quick Check:

    Path compression updates parent recursively = A [OK]
Hint: Look for parent update inside find recursion [OK]
Common Mistakes:
  • Using loop without updating parent (Option B)
  • Not updating parent during recursion (Option C)
  • Resetting parent incorrectly (Option D)
3. Given the following Union Find structure with parent = [0, 0, 1, 2, 3], what will be the parent array after calling find(4) with path compression?
medium
A. [0, 0, 0, 0, 0]
B. [0, 0, 1, 2, 0]
C. [0, 1, 1, 2, 3]
D. [0, 0, 1, 3, 3]

Solution

  1. Step 1: Trace find(4) path

    4's parent is 3, 3's parent is 2, 2's parent is 1, 1's parent is 0, which is root.
  2. Step 2: Apply path compression

    All nodes on path (4,3,2,1) update parent to root 0 (parent[1] already 0). parent[2]=0, parent[3]=0, parent[4]=0. So parent array becomes [0,0,0,0,0].
  3. Final Answer:

    [0, 0, 0, 0, 0] -> Option A
  4. Quick Check:

    Path compression sets parent[4] = 0 = A [OK]
Hint: Update all parents on path to root after find [OK]
Common Mistakes:
  • Only updating parent[4] to root (Option B)
  • Not updating any parent (Option C)
  • Updating wrong parents (Option D)
4. Identify the bug in this find function with path compression:
int find(int x) {
  if(parent[x] == x) return x;
  else {
    find(parent[x]);
    parent[x] = parent[parent[x]];
    return parent[x];
  }
}
medium
A. No bug, code is correct
B. Incorrect base case condition
C. Missing return value from recursive call before assignment
D. Parent update should be after return

Solution

  1. Step 1: Analyze recursive call

    The recursive call find(parent[x]) is made but its return value is ignored.
  2. Step 2: Check parent update

    Parent update uses parent[parent[x]] instead of the root returned by recursion, so path compression fails.
  3. Final Answer:

    Missing return value from recursive call before assignment -> Option C
  4. Quick Check:

    Return value must be assigned to parent[x] = D [OK]
Hint: Assign recursive find result to parent[x] for compression [OK]
Common Mistakes:
  • Ignoring recursive return value
  • Updating parent incorrectly
  • Misunderstanding base case
5. You have a Union Find with 5 elements and parent = [0, 0, 1, 2, 3]. After calling union(4, 0) and then find(4) with path compression, what is the final parent array?
hard
A. [0, 0, 1, 2, 0]
B. [0, 0, 1, 2, 4]
C. [0, 0, 0, 0, 0]
D. [0, 1, 1, 2, 0]

Solution

  1. Step 1: Perform union(4, 0)

    Find roots: root(4) = 4 (chain 4->3->2->1->0), root(0) = 0. Since roots differ, link root(4) to root(0). So parent[4] = 0. Updated parent = [0, 0, 1, 2, 0].
  2. Step 2: Call find(4) with path compression

    Path: 4 -> 0. Since 0 is root, parent[4] already 0, no further updates. Final parent = [0, 0, 1, 2, 0].
  3. Final Answer:

    [0, 0, 1, 2, 0] -> Option A
  4. Quick Check:

    Union links 4 to 0, find compresses path = A [OK]
Hint: Union links roots, find compresses path to root [OK]
Common Mistakes:
  • Incorrectly linking assuming same root (Option C)
  • Wrong union target (Option B)
  • Incomplete path compression (Option D)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard