Challenge - 5 Problems
Topological Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Topological Sort on a Simple DAG
What is the output of the following Java code performing topological sort using DFS on the given graph?
DSA Java
import java.util.*; public class Main { static void dfs(int node, boolean[] visited, Stack<Integer> stack, List<List<Integer>> graph) { visited[node] = true; for (int adj : graph.get(node)) { if (!visited[adj]) { dfs(adj, visited, stack, graph); } } stack.push(node); } public static void main(String[] args) { int V = 4; List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < V; i++) graph.add(new ArrayList<>()); graph.get(0).add(1); graph.get(0).add(2); graph.get(1).add(2); graph.get(2).add(3); boolean[] visited = new boolean[V]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < V; i++) { if (!visited[i]) dfs(i, visited, stack, graph); } while (!stack.isEmpty()) { System.out.print(stack.pop() + " "); } } }
Attempts:
2 left
💡 Hint
Remember that in DFS-based topological sort, nodes are pushed to stack after visiting all descendants.
✗ Incorrect
The DFS visits nodes starting from 0, exploring 1 and 2 before 3. The stack ends up with 3 on top, then 2, then 1, then 0 at bottom. Popping gives 3 2 1 0.
🧠 Conceptual
intermediateUnderstanding the Role of Stack in DFS Topological Sort
In DFS-based topological sort, why do we push the node onto the stack only after visiting all its adjacent nodes?
Attempts:
2 left
💡 Hint
Think about the order in which nodes should appear to respect dependency directions.
✗ Incorrect
Pushing nodes after visiting all neighbors ensures that dependent nodes come before the node itself in the final order, which is why popping from the stack gives a valid topological order.
🔧 Debug
advancedIdentify the Error in DFS Topological Sort Implementation
What error will occur when running this code snippet for topological sort using DFS?
DSA Java
void dfs(int node, boolean[] visited, Stack<Integer> stack, List<List<Integer>> graph) { visited[node] = true; for (int adj : graph.get(node)) { if (visited[adj]) { dfs(adj, visited, stack, graph); } } stack.push(node); }
Attempts:
2 left
💡 Hint
Check the condition that decides when to recurse on adjacent nodes.
✗ Incorrect
The condition 'if (visited[adj])' recurses on already visited nodes, causing infinite recursion and eventually StackOverflowError.
❓ Predict Output
advancedOutput of Topological Sort on Graph with Multiple Independent Chains
What is the output of the following Java code performing topological sort using DFS on the given graph with two independent chains?
DSA Java
import java.util.*; public class Main { static void dfs(int node, boolean[] visited, Stack<Integer> stack, List<List<Integer>> graph) { visited[node] = true; for (int adj : graph.get(node)) { if (!visited[adj]) dfs(adj, visited, stack, graph); } stack.push(node); } public static void main(String[] args) { int V = 6; List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < V; i++) graph.add(new ArrayList<>()); graph.get(0).add(1); graph.get(1).add(2); graph.get(3).add(4); graph.get(4).add(5); boolean[] visited = new boolean[V]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < V; i++) { if (!visited[i]) dfs(i, visited, stack, graph); } while (!stack.isEmpty()) { System.out.print(stack.pop() + " "); } } }
Attempts:
2 left
💡 Hint
Consider the order in which nodes are visited and pushed to the stack for independent chains.
✗ Incorrect
DFS visits nodes from 0 to 5. It finishes chain 0->1->2 first, pushing 2,1,0, then visits chain 3->4->5 pushing 5,4,3. Popping stack gives 3 4 5 0 1 2.
🧠 Conceptual
expertDetecting Cycles in Directed Graph Using DFS for Topological Sort
Which approach correctly detects a cycle in a directed graph during DFS-based topological sort?
Attempts:
2 left
💡 Hint
Think about how to detect back edges during DFS traversal.
✗ Incorrect
Using a recursion stack helps detect back edges indicating cycles. If a node is found in the recursion stack during DFS, a cycle exists.
