Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the queue used in BFS.
DSA C++
std::queue<[1]> q;Attempts:
3 left
💡 Hint
Common Mistakes
Using string or float instead of int for node representation.
Using stack instead of queue.
✗ Incorrect
The BFS queue stores nodes represented as integers.
2fill in blank
mediumComplete the code to initialize the distance vector with -1.
DSA C++
std::vector<int> dist(n, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Initializing distance with 0 which means visited.
Using INT_MAX which is not needed here.
✗ Incorrect
Distance is initialized to -1 to indicate unvisited nodes.
3fill in blank
hardFix the error in the BFS loop condition to process nodes while queue is not empty.
DSA C++
while (!q.[1]()) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using q.front() as condition which returns element, not boolean.
Using q.pop() which removes element, not checks emptiness.
✗ Incorrect
The loop continues while the queue is not empty, checked by q.empty().
4fill in blank
hardFill both blanks to update distance and enqueue neighbor nodes correctly.
DSA C++
if (dist[neighbor] == [1]) { dist[neighbor] = dist[current] [2] 1; q.push(neighbor); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of -1 to check unvisited.
Subtracting 1 instead of adding 1 to distance.
✗ Incorrect
Unvisited neighbors have distance -1; update distance by adding 1 to current node's distance.
5fill in blank
hardFill all three blanks to reconstruct the shortest path from target to source.
DSA C++
std::vector<int> path; int node = target; while (node != [1]) { path.push_back(node); node = [2][node]; } path.push_back([3]); std::reverse(path.begin(), path.end());
Attempts:
3 left
💡 Hint
Common Mistakes
Using dist array instead of parent to trace path.
Not adding source node to path before reversing.
✗ Incorrect
Stop when node equals source; update node to its parent; finally add source to path.
