Challenge - 5 Problems
BFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BFS traversal on a simple graph
What is the output of the BFS traversal starting from node 0 in the given graph?
DSA C++
/* Graph edges: 0 -> 1, 2 1 -> 2 2 -> 0, 3 3 -> 3 */ #include <iostream> #include <vector> #include <queue> void bfs(int start, const std::vector<std::vector<int>>& graph) { std::vector<bool> visited(graph.size(), false); std::queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int node = q.front(); q.pop(); std::cout << node << " "; for (int neighbor : graph[node]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } } } int main() { std::vector<std::vector<int>> graph = { {1, 2}, {2}, {0, 3}, {3} }; bfs(0, graph); return 0; }
Attempts:
2 left
💡 Hint
Remember BFS visits neighbors level by level, starting from the start node.
✗ Incorrect
Starting from node 0, BFS visits neighbors 1 and 2 in order. Then from 1, it visits 2 but 2 is already visited. From 2, it visits 3. So the order is 0 1 2 3.
❓ Predict Output
intermediateBFS traversal output on disconnected graph
What is the output of BFS traversal starting from node 1 in this disconnected graph?
DSA C++
#include <iostream> #include <vector> #include <queue> void bfs(int start, const std::vector<std::vector<int>>& graph) { std::vector<bool> visited(graph.size(), false); std::queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int node = q.front(); q.pop(); std::cout << node << " "; for (int neighbor : graph[node]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } } } int main() { std::vector<std::vector<int>> graph = { {1}, {}, {3}, {} }; bfs(1, graph); return 0; }
Attempts:
2 left
💡 Hint
Check which nodes are reachable from node 1.
✗ Incorrect
Node 1 has no neighbors, so BFS visits only node 1.
🔧 Debug
advancedIdentify the error in BFS implementation
What error will this BFS code produce when run on the given graph?
DSA C++
#include <iostream> #include <vector> #include <queue> void bfs(int start, const std::vector<std::vector<int>>& graph) { std::vector<bool> visited(graph.size(), false); std::queue<int> q; q.push(start); // Missing visited[start] = true; while (!q.empty()) { int node = q.front(); q.pop(); std::cout << node << " "; for (int neighbor : graph[node]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } } } int main() { std::vector<std::vector<int>> graph = { {1}, {2}, {} }; bfs(0, graph); return 0; }
Attempts:
2 left
💡 Hint
Check if the start node is marked visited before starting the loop.
✗ Incorrect
Since visited[start] is not set true initially, the start node is re-added repeatedly causing infinite loop.
🧠 Conceptual
advancedBFS uses which data structure to keep track of nodes to visit?
Which data structure is used internally by BFS to keep track of nodes to visit next?
Attempts:
2 left
💡 Hint
BFS visits nodes level by level in order of discovery.
✗ Incorrect
BFS uses a queue to process nodes in the order they are discovered, ensuring level order traversal.
🚀 Application
expertShortest path length using BFS in unweighted graph
Given an unweighted graph, which BFS modification helps find the shortest path length from start node to all other nodes?
Attempts:
2 left
💡 Hint
Shortest path in unweighted graph is found by levels of BFS.
✗ Incorrect
By tracking distance from start node and updating neighbors with distance +1 when first visited, BFS finds shortest paths.
