Challenge - 5 Problems
DFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of DFS traversal on a simple graph
What is the output of the following DFS traversal starting from node 0?
DSA C++
/* Graph adjacency list representation: 0: 1, 2 1: 2 2: 0, 3 3: 3 */ #include <iostream> #include <vector> using namespace std; void dfs(int node, vector<vector<int>>& graph, vector<bool>& visited) { visited[node] = true; cout << node << " "; for (int neighbor : graph[node]) { if (!visited[neighbor]) { dfs(neighbor, graph, visited); } } } int main() { vector<vector<int>> graph = { {1, 2}, {2}, {0, 3}, {3} }; vector<bool> visited(4, false); dfs(0, graph, visited); return 0; }
Attempts:
2 left
💡 Hint
Trace the DFS calls starting from node 0 and follow neighbors in order.
✗ Incorrect
The DFS starts at node 0, visits 1 first (the first neighbor), then from 1 visits 2, and from 2 visits 3. The output is the order nodes are first visited.
🧠 Conceptual
intermediateDFS stack behavior on graph traversal
Which data structure does DFS use implicitly or explicitly to keep track of nodes to visit next?
Attempts:
2 left
💡 Hint
Think about the order in which nodes are revisited in DFS.
✗ Incorrect
DFS uses a stack (explicitly or via recursion call stack) to remember nodes to visit next, exploring as deep as possible before backtracking.
🔧 Debug
advancedIdentify the error in DFS implementation
What error will occur when running this DFS code on a graph with cycles?
DSA C++
#include <iostream> #include <vector> using namespace std; void dfs(int node, vector<vector<int>>& graph) { cout << node << " "; for (int neighbor : graph[node]) { dfs(neighbor, graph); } } int main() { vector<vector<int>> graph = { {1}, {2}, {0} }; dfs(0, graph); return 0; }
Attempts:
2 left
💡 Hint
Consider what happens when the graph has a cycle and no visited check.
✗ Incorrect
The DFS does not track visited nodes, so it revisits nodes endlessly in a cycle, causing infinite recursion and stack overflow.
❓ Predict Output
advancedDFS traversal order with multiple neighbors
What is the output of DFS starting from node 1 on this graph?
DSA C++
/* Graph adjacency list: 0: 2, 3 1: 0, 4 2: 3 3: 4 4: */ #include <iostream> #include <vector> using namespace std; void dfs(int node, vector<vector<int>>& graph, vector<bool>& visited) { visited[node] = true; cout << node << " "; for (int neighbor : graph[node]) { if (!visited[neighbor]) { dfs(neighbor, graph, visited); } } } int main() { vector<vector<int>> graph = { {2, 3}, {0, 4}, {3}, {4}, {} }; vector<bool> visited(5, false); dfs(1, graph, visited); return 0; }
Attempts:
2 left
💡 Hint
Follow neighbors in order and mark visited nodes carefully.
✗ Incorrect
Starting at 1, DFS visits 0 first, then from 0 visits 2, then from 2 visits 3, and finally from 3 visits 4.
🧠 Conceptual
expertDFS time complexity on graph with V vertices and E edges
What is the time complexity of DFS traversal on a graph with V vertices and E edges?
Attempts:
2 left
💡 Hint
Consider how many times each vertex and edge is visited during DFS.
✗ Incorrect
DFS visits each vertex once and explores each edge once, so total time is proportional to vertices plus edges.
