Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add the starting node to the queue in BFS.
DSA C++
std::queue<int> q;
q.[1](start);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push to add elements.
Using front which only accesses the first element.
Using empty which checks if the queue is empty.
✗ Incorrect
In BFS, we add the starting node to the queue using the push() method.
2fill in blank
mediumComplete the code to mark a node as visited in BFS.
DSA C++
std::vector<bool> visited(n, false);
visited[[1]] = true;Attempts:
3 left
💡 Hint
Common Mistakes
Marking the queue or adjacency list as visited.
Using a variable not representing a node index.
✗ Incorrect
We mark the starting node as visited by setting visited[start] = true.
3fill in blank
hardFix the error in the BFS loop to get the current node from the queue.
DSA C++
while (!q.empty()) { int current = q.[1](); q.pop(); // process current }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() to get the element, which returns void.
Using push() or empty() incorrectly.
✗ Incorrect
To get the current node without removing it, use front(). Then pop() removes it.
4fill in blank
hardFill both blanks to check if a neighbor is unvisited and then mark it visited.
DSA C++
for (int neighbor : adj[current]) { if (!visited[[1]]) { visited[[2]] = true; q.push(neighbor); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using current or start instead of neighbor.
Using adjacency list variable instead of node index.
✗ Incorrect
We check and mark the neighbor node as visited before adding it to the queue.
5fill in blank
hardFill all three blanks to complete BFS that prints nodes in order.
DSA C++
std::queue<int> q; std::vector<bool> visited(n, false); q.[1](start); visited[[2]] = true; while (!q.empty()) { int current = q.[3](); q.pop(); std::cout << current << " "; for (int neighbor : adj[current]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() to get current node.
Not marking start node visited.
Using wrong variable names.
✗ Incorrect
We push the start node, mark it visited, then get current node with front() in BFS.
