Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the edge list vector.
DSA C++
std::vector<std::pair<int, std::pair<int, int>>> [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not represent edges.
Using a name that implies nodes or weights only.
✗ Incorrect
The vector named edges stores all edges as pairs of weight and node pairs.
2fill in blank
mediumComplete the code to sort edges by their weight.
DSA C++
std::sort([1].begin(), [1].end());
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting the wrong vector like nodes or weights.
Not sorting at all.
✗ Incorrect
We sort the edges vector to process edges from smallest to largest weight.
3fill in blank
hardFix the error in the union-find find function to correctly find the parent.
DSA C++
int findParent(int node, std::vector<int>& parent) {
if (parent[node] == [1]) {
return node;
}
return parent[node] = findParent(parent[node], parent);
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking if parent[node] equals node or 0 instead of -1.
Returning wrong values causing infinite recursion.
✗ Incorrect
The root parent is identified by parent[node] == -1 meaning no parent above.
4fill in blank
hardFill both blanks to correctly union two sets by their parents.
DSA C++
void unionSets(int u, int v, std::vector<int>& parent) {
int parentU = findParent(u, parent);
int parentV = findParent(v, parent);
if (parentU != parentV) {
parent[[1]] = [2];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using node variables instead of their parents.
Assigning in the wrong direction causing cycles.
✗ Incorrect
We set the parent of parentU to parentV to merge the sets.
5fill in blank
hardFill all three blanks to add edges to MST and union their sets.
DSA C++
for (auto& edge : edges) { int weight = edge.first; int u = edge.second.first; int v = edge.second.second; if (findParent(u, parent) != findParent(v, parent)) { mst.push_back([1]); unionSets([2], [3], parent); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding only weight or nodes instead of the full edge.
Unioning wrong nodes or using weight instead of nodes.
✗ Incorrect
We add the whole edge to MST and union the sets of u and v.
