Bird
Raised Fist0
DSA Javaprogramming~20 mins

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

Choose your learning style10 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 find operation with path compression
What is the output of the following Java code snippet that uses path compression in Union Find?
DSA Java
class UnionFind {
    int[] parent;
    public UnionFind(int n) {
        parent = new int[n];
        for (int i = 0; i < n; i++) parent[i] = i;
    }
    public int find(int x) {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }
    public void union(int a, int b) {
        int rootA = find(a);
        int rootB = find(b);
        if (rootA != rootB) parent[rootB] = rootA;
    }
}

public class Main {
    public static void main(String[] args) {
        UnionFind uf = new UnionFind(5);
        uf.union(0, 1);
        uf.union(1, 2);
        System.out.println(uf.find(2));
        System.out.println(java.util.Arrays.toString(uf.parent));
    }
}
A
0
[0, 1, 0, 3, 4]
B
1
[0, 0, 1, 3, 4]
C
0
[0, 0, 0, 3, 4]
D
2
[0, 1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Remember that path compression updates the parent of all nodes on the find path to the root.
🧠 Conceptual
intermediate
1:30remaining
Effect of path compression on parent array
Given a Union Find structure with parent array [0, 0, 1, 3, 4], what will be the parent array after calling find(2) with path compression?
A[0, 0, 1, 4, 4]
B[0, 1, 1, 3, 4]
C[0, 0, 2, 3, 4]
D[0, 0, 0, 3, 4]
Attempts:
2 left
💡 Hint
Path compression sets the parent of the node directly to the root.
🔧 Debug
advanced
1:30remaining
Identify the bug in path compression implementation
What error will occur when running this Union Find find method with path compression?
DSA Java
public int find(int x) {
    if (parent[x] == x) {
        return x;
    }
    parent[x] = find(parent[x]);
    return parent[x];
}
ANo error, works correctly
BStackOverflowError due to infinite recursion
CCompilation error: missing return statement
DNullPointerException at runtime
Attempts:
2 left
💡 Hint
Check if all code paths return a value.
❓ Predict Output
advanced
2:00remaining
Output after multiple unions and finds with path compression
What is the output of the parent array after these operations?
DSA Java
UnionFind 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);
System.out.println(java.util.Arrays.toString(uf.parent));
A[0, 1, 2, 3, 4, 5]
B[0, 0, 0, 0, 3, 0]
C[0, 0, 0, 3, 3, 3]
D[0, 0, 0, 0, 0, 0]
Attempts:
2 left
💡 Hint
Trace unions carefully and remember path compression updates parents on find.
🧠 Conceptual
expert
1:30remaining
Time complexity impact of path compression in Union Find
What is the amortized time complexity of the find operation with path compression in a Union Find data structure?
AO(α(n)) amortized, where α is the inverse Ackermann function
BO(1) amortized
CO(n) worst case
DO(log n) amortized
Attempts:
2 left
💡 Hint
Path compression combined with union by rank gives almost constant time.

Practice

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

Solution

  1. Step 1: Understand the role of path compression

    Path compression is used during the find operation to make future queries faster by making nodes point directly to the root.
  2. Step 2: Identify the effect on the tree structure

    This flattens the tree, reducing the height and speeding up subsequent find calls.
  3. Final Answer:

    To speed up the find operation by flattening the tree structure -> Option B
  4. Quick Check:

    Path compression = flatten tree for faster find [OK]
Hint: Path compression flattens trees to speed up find [OK]
Common Mistakes:
  • Confusing path compression with union by size/rank
  • Thinking it merges sets instead of optimizing find
  • Assuming it initializes data structures
2. Which of the following is the correct Java method signature for the find function implementing path compression in Union Find?
easy
A. public int find(int x)
B. public void find(int x)
C. public boolean find(int x)
D. public int find()"

Solution

  1. Step 1: Understand the return type of find

    The find method returns the representative (root) of the set containing element x, so it returns an integer.
  2. Step 2: Check method parameters

    The method must accept the element x as input to find its root.
  3. Final Answer:

    public int find(int x) -> Option A
  4. Quick Check:

    find returns root index as int [OK]
Hint: find returns int root and takes int parameter [OK]
Common Mistakes:
  • Using void return type for find
  • Missing parameter for element to find
  • Returning boolean instead of int
3. Consider the following Java code snippet implementing path compression in Union Find:
int[] parent = {0, 0, 1, 2, 3};

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

System.out.println(find(4));
System.out.println(Arrays.toString(parent));
What is the output?
medium
A. 4 [0, 0, 1, 2, 3]
B. 0 [0, 0, 1, 2, 3]
C. 0 [0, 0, 0, 0, 0]
D. 0 [0, 0, 1, 2, 0]

Solution

  1. Step 1: Trace find(4) call with path compression

    parent[4]=3 -> find(3); parent[3]=2 -> find(2); parent[2]=1 -> find(1); parent[1]=0 -> find(0)=0 (base); parent[1]=0; parent[2]=0; parent[3]=0; parent[4]=0. Returns 0.
  2. Step 2: Update parent array after path compression

    Entire path flattens: parent = [0, 0, 0, 0, 0].
  3. Final Answer:

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

    find(4) = 0, all path nodes point to root 0 [OK]
Hint: Path compression flattens entire path to root [OK]
Common Mistakes:
  • Thinking only leaf updates (partial compression)
  • Expecting no change in parent array
  • Misreading recursion depth or base case
4. The following find method is intended to implement path compression but has a bug:
int find(int x) {
    if (parent[x] == x) {
        return x;
    }
    return find(parent[x]);
}
What is the bug and how to fix it?
medium
A. Missing path compression assignment; fix by adding parent[x] = find(parent[x]);
B. Incorrect base case; fix by changing parent[x] == x to parent[x] != x
C. Wrong return type; fix by changing return type to void
D. No bug; code is correct

Solution

  1. Step 1: Identify missing path compression step

    The method recursively finds the root but does not update parent[x] to root, so no path compression happens.
  2. Step 2: Fix by assigning compressed parent

    Adding parent[x] = find(parent[x]); before returning compresses the path.
  3. Final Answer:

    Missing path compression assignment; fix by adding parent[x] = find(parent[x]); -> Option A
  4. Quick Check:

    Assign parent[x] = find(parent[x]) for compression [OK]
Hint: Always assign parent[x] = find(parent[x]) to compress path [OK]
Common Mistakes:
  • Forgetting to update parent[x]
  • Changing base case incorrectly
  • Returning void instead of int
5. Given a Union Find structure with parent = {0, 0, 1, 2, 4, 5} and rank = {0, 1, 2, 3, 4, 5}, after performing union(4, 5) and then find(5) with path compression, what will be the updated parent array?
hard
A. [0, 0, 1, 2, 4, 4]
B. [0, 0, 1, 2, 3, 4]
C. [0, 0, 1, 2, 3, 3]
D. [0, 0, 1, 2, 5, 5]

Solution

  1. Step 1: Perform union(4, 5) by rank

    Ranks: rank[4]=4, rank[5]=5. Since rank[5] > rank[4], parent[4] = 5. But ranks given are increasing, so actually rank[5] is highest, so parent[4] = 5.
  2. Step 2: Perform find(5) with path compression

    find(5) returns 5 (root). Path compression updates parent of 5 to itself (no change). But parent[4] points to 5, so parent array is [0,0,1,2,5,5].
  3. Step 3: Check options

    [0, 0, 1, 2, 4, 4] shows parent[5] = 4, which contradicts union by rank. [0, 0, 1, 2, 3, 4] no change. [0, 0, 1, 2, 3, 3] parent[5]=3 incorrect. [0, 0, 1, 2, 5, 5] parent[4]=5 and parent[5]=5 matches expected.
  4. Final Answer:

    [0, 0, 1, 2, 5, 5] -> Option D
  5. Quick Check:

    Union by rank sets parent of lower rank to higher [OK]
Hint: Union by rank attaches lower rank root to higher rank root [OK]
Common Mistakes:
  • Confusing which parent to update in union
  • Ignoring rank comparison
  • Assuming parent array unchanged after union
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