Challenge - 5 Problems
DFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateDFS traversal order on a simple graph
What is the output order of nodes visited by the DFS starting from node 0?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, List<int>> graph = new Dictionary<int, List<int>> { {0, new List<int>{1, 2}}, {1, new List<int>{3}}, {2, new List<int>{4}}, {3, new List<int>()}, {4, new List<int>()} }; static HashSet<int> visited = new HashSet<int>(); static void DFS(int node) { Console.Write(node + " "); visited.Add(node); foreach (var neighbor in graph[node]) { if (!visited.Contains(neighbor)) { DFS(neighbor); } } } static void Main() { DFS(0); } }
Attempts:
2 left
💡 Hint
Remember DFS explores as far as possible along each branch before backtracking.
✗ Incorrect
Starting at node 0, DFS visits 0, then goes to 1, then 3 (no neighbors), backtracks to 0, then visits 2, then 4.
❓ Predict Output
intermediateDFS visit count on disconnected graph
How many nodes will be visited by DFS starting from node 1 in this graph?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, List<int>> graph = new Dictionary<int, List<int>> { {0, new List<int>{1}}, {1, new List<int>{2}}, {2, new List<int>()}, {3, new List<int>{4}}, {4, new List<int>()} }; static HashSet<int> visited = new HashSet<int>(); static void DFS(int node) { visited.Add(node); foreach (var neighbor in graph[node]) { if (!visited.Contains(neighbor)) { DFS(neighbor); } } } static void Main() { DFS(1); Console.WriteLine(visited.Count); } }
Attempts:
2 left
💡 Hint
DFS only visits nodes reachable from the start node.
✗ Incorrect
Starting at node 1, DFS visits nodes 1 and 2. Node 0 is not visited because DFS does not go backwards. So total visited nodes are 2.
🔧 Debug
advancedIdentify the error in DFS implementation
What error will this DFS code produce when run?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, List<int>> graph = new Dictionary<int, List<int>> { {0, new List<int>{1}}, {1, new List<int>{2}}, {2, new List<int>()} }; static HashSet<int> visited = new HashSet<int>(); static void DFS(int node) { if (visited.Contains(node)) return; foreach (var neighbor in graph[node]) { DFS(neighbor); } visited.Add(node); } static void Main() { DFS(0); Console.WriteLine(string.Join(",", visited)); } }
Attempts:
2 left
💡 Hint
Check when nodes are marked visited in DFS.
✗ Incorrect
Nodes are added to visited after visiting neighbors, so the output order is post-order: 2,1,0.
❓ Predict Output
advancedDFS with cycle detection output
What will be printed by this DFS code that detects cycles?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, List<int>> graph = new Dictionary<int, List<int>> { {0, new List<int>{1}}, {1, new List<int>{2}}, {2, new List<int>{0}} }; static HashSet<int> visited = new HashSet<int>(); static HashSet<int> recursionStack = new HashSet<int>(); static bool hasCycle = false; static void DFS(int node) { if (recursionStack.Contains(node)) { hasCycle = true; return; } if (visited.Contains(node)) return; visited.Add(node); recursionStack.Add(node); foreach (var neighbor in graph[node]) { DFS(neighbor); } recursionStack.Remove(node); } static void Main() { DFS(0); Console.WriteLine(hasCycle); } }
Attempts:
2 left
💡 Hint
A cycle exists if a node is revisited in the current recursion path.
✗ Incorrect
The graph has a cycle 0->1->2->0, so hasCycle becomes true and prints True.
🧠 Conceptual
expertDFS stack vs recursion behavior
Which statement about DFS implementation using explicit stack vs recursion is correct?
Attempts:
2 left
💡 Hint
Think about how the order of neighbor processing affects traversal.
✗ Incorrect
Both recursive and stack-based DFS visit nodes in the same order if neighbors are processed in the same order because the stack simulates recursion.
