Challenge - 5 Problems
Union Find Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of find operation with path compression
What is the output of the following C++ code snippet that uses path compression in Union Find?
DSA C++
class UnionFind { public: std::vector<int> parent; UnionFind(int n) { parent.resize(n); for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void unite(int a, int b) { int rootA = find(a); int rootB = find(b); if (rootA != rootB) { parent[rootB] = rootA; } } }; int main() { UnionFind uf(5); uf.unite(0, 1); uf.unite(1, 2); uf.unite(3, 4); int root = uf.find(2); std::string result = "parent array: "; for (int i = 0; i < 5; i++) { result += std::to_string(uf.parent[i]) + " "; } std::cout << result << std::endl; std::cout << "root of 2: " << root << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that path compression updates the parent of all nodes on the path to the root.
✗ Incorrect
After uniting 0-1 and 1-2, all three nodes 0,1,2 share the same root 0. The find(2) call compresses the path, making parent[2] point directly to 0. The parent array reflects this with parent[0]=0, parent[1]=0, parent[2]=0, and the other sets unchanged.
🧠 Conceptual
intermediateEffect of path compression on find operation
Which of the following best describes the effect of path compression in the find operation of Union Find?
Attempts:
2 left
💡 Hint
Think about how path compression changes the parent links during find.
✗ Incorrect
Path compression makes all nodes on the find path point directly to the root, flattening the structure and making future find operations faster.
🔧 Debug
advancedIdentify the error in this Union Find implementation with path compression
What error will the following code produce when calling find(2)?
DSA C++
class UnionFind { public: std::vector<int> parent; UnionFind(int n) { parent.resize(n); for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] == x) { return x; } return parent[x] = find(parent[x]); } }; int main() { UnionFind uf(3); uf.parent = {0, 0, 1}; int root = uf.find(2); std::cout << "root: " << root << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check if all code paths in find return a value.
✗ Incorrect
The find function lacks a return statement after the recursive call, causing a compilation error.
❓ Predict Output
advancedFinal parent array after multiple unions and finds with path compression
What is the final state of the parent array after the following operations?
DSA C++
class UnionFind { public: std::vector<int> parent; UnionFind(int n) { parent.resize(n); for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void unite(int a, int b) { int rootA = find(a); int rootB = find(b); if (rootA != rootB) { parent[rootB] = rootA; } } }; int main() { UnionFind uf(6); uf.unite(0, 1); uf.unite(1, 2); uf.unite(3, 4); uf.unite(4, 5); uf.find(5); uf.unite(2, 5); uf.find(5); std::string result = "parent array: "; for (int i = 0; i < 6; i++) { result += std::to_string(uf.parent[i]) + " "; } std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that find with path compression updates parents along the path.
✗ Incorrect
After all unions and finds, the entire set is connected with root 0. Path compression updates parent[3] and parent[5] to 0, making the parent array: 0 0 0 0 3 0.
🧠 Conceptual
expertTime complexity impact of path compression in Union Find
What is the amortized time complexity of a find operation with path compression in a Union Find data structure?
Attempts:
2 left
💡 Hint
Path compression flattens the tree structure over time.
✗ Incorrect
With path compression, the amortized time per find operation is almost constant, often described as inverse Ackermann function, which is very slow growing and practically constant.
