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 Java
import java.util.*; class Graph { private final Map<Integer, List<Integer>> adjList = new HashMap<>(); void addEdge(int u, int v) { adjList.computeIfAbsent(u, k -> new ArrayList<>()).add(v); adjList.computeIfAbsent(v, k -> new ArrayList<>()).add(u); } void dfs(int start, Set<Integer> visited, List<Integer> result) { visited.add(start); result.add(start); for (int neighbor : adjList.getOrDefault(start, Collections.emptyList())) { if (!visited.contains(neighbor)) { dfs(neighbor, visited, result); } } } } public class Main { public static void main(String[] args) { Graph g = new Graph(); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 3); g.addEdge(2, 4); List<Integer> result = new ArrayList<>(); g.dfs(0, new HashSet<>(), result); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Remember DFS explores as far as possible along each branch before backtracking.
✗ Incorrect
Starting from 0, DFS visits 1 first (from adjacency list order), then 3 from 1, backtracks to 0, then visits 2, and finally 4 from 2.
🧠 Conceptual
intermediateDFS stack vs recursion behavior
Which statement best describes how DFS uses the call stack or an explicit stack during traversal?
Attempts:
2 left
💡 Hint
Think about how DFS goes deep before exploring siblings.
✗ Incorrect
DFS explores as far as possible along each branch before backtracking, which is naturally implemented using a stack structure.
🔧 Debug
advancedIdentify the error in DFS implementation
What error will this DFS code produce when run on a graph with cycles?
DSA Java
void dfs(int node) { System.out.print(node + " "); for (int neighbor : adjList.get(node)) { dfs(neighbor); } }
Attempts:
2 left
💡 Hint
Think about what happens if a node is visited multiple times without tracking.
✗ Incorrect
Without marking visited nodes, DFS will recurse infinitely on cycles causing a stack overflow.
🚀 Application
advancedDFS to find connected components count
Given an undirected graph, which code snippet correctly counts the number of connected components using DFS?
DSA Java
int countComponents(int n, int[][] edges) { Map<Integer, List<Integer>> graph = new HashMap<>(); for (int i = 0; i < n; i++) graph.put(i, new ArrayList<>()); for (int[] e : edges) { graph.get(e[0]).add(e[1]); graph.get(e[1]).add(e[0]); } boolean[] visited = new boolean[n]; int count = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { dfs(i, graph, visited); count++; } } return count; } void dfs(int node, Map<Integer, List<Integer>> graph, boolean[] visited) { visited[node] = true; for (int neighbor : graph.get(node)) { if (!visited[neighbor]) dfs(neighbor, graph, visited); } }
Attempts:
2 left
💡 Hint
Each DFS call from an unvisited node marks one component.
✗ Incorrect
The code initializes the graph, uses DFS to mark visited nodes, and counts how many times DFS starts, which equals connected components.
❓ Predict Output
expertOutput of DFS on directed graph with back edges
What is the output of the DFS traversal starting from node 1 on this directed graph?
DSA Java
import java.util.*; class DirectedGraph { private final Map<Integer, List<Integer>> adjList = new HashMap<>(); void addEdge(int u, int v) { adjList.computeIfAbsent(u, k -> new ArrayList<>()).add(v); } void dfs(int node, Set<Integer> visited, List<Integer> result) { visited.add(node); result.add(node); for (int neighbor : adjList.getOrDefault(node, Collections.emptyList())) { if (!visited.contains(neighbor)) { dfs(neighbor, visited, result); } } } } public class Main { public static void main(String[] args) { DirectedGraph g = new DirectedGraph(); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(3, 1); // back edge creating a cycle g.addEdge(2, 4); List<Integer> result = new ArrayList<>(); g.dfs(1, new HashSet<>(), result); System.out.println(result); } }
Attempts:
2 left
💡 Hint
DFS avoids revisiting nodes already visited, even in cycles.
✗ Incorrect
Starting at 1, DFS visits 2, then 3, then back edge to 1 is ignored, then visits 4 from 2. So order is [1, 2, 3, 4].
