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 Go code performing topological sort using DFS on a directed acyclic graph (DAG)?
DSA Go
package main import "fmt" func topoSortDFS(graph map[int][]int) []int { visited := make(map[int]bool) stack := []int{} var dfs func(int) dfs = func(node int) { visited[node] = true for _, neighbor := range graph[node] { if !visited[neighbor] { dfs(neighbor) } } stack = append([]int{node}, stack...) } for node := range graph { if !visited[node] { dfs(node) } } return stack } func main() { graph := map[int][]int{ 5: {2, 0}, 4: {0, 1}, 2: {3}, 3: {1}, 0: {}, 1: {}, } result := topoSortDFS(graph) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Remember that in DFS-based topological sort, nodes are added to the stack after visiting all their neighbors.
✗ Incorrect
The DFS visits nodes starting from 5 and 4. Node 5 leads to 2 and 0, and 2 leads to 3, which leads to 1. The order of finishing times results in [5 4 2 3 1 0].
🧠 Conceptual
intermediateUnderstanding the Role of the Stack in DFS Topological Sort
In a DFS-based topological sort, why do we add nodes to the stack only after visiting all their neighbors?
Attempts:
2 left
💡 Hint
Think about the order in which tasks depending on others should appear.
✗ Incorrect
Adding nodes after visiting neighbors ensures that all nodes they depend on are placed earlier in the order, respecting dependencies.
🔧 Debug
advancedIdentify the Bug in This DFS Topological Sort Implementation
What error will this Go code produce when running topological sort using DFS on a graph with a cycle?
DSA Go
package main import "fmt" func topoSortDFS(graph map[int][]int) []int { visited := make(map[int]bool) stack := []int{} var dfs func(int) dfs = func(node int) { visited[node] = true for _, neighbor := range graph[node] { dfs(neighbor) } stack = append([]int{node}, stack...) } for node := range graph { if !visited[node] { dfs(node) } } return stack } func main() { graph := map[int][]int{ 1: {2}, 2: {3}, 3: {1}, } result := topoSortDFS(graph) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about what happens when DFS revisits nodes in a cycle without cycle detection.
✗ Incorrect
Without cycle detection, DFS revisits nodes endlessly in the cycle, causing infinite recursion and stack overflow.
📝 Syntax
advancedFind the Syntax Error in This DFS Topological Sort Snippet
Which option contains the correct syntax to prepend an element to a slice in Go during DFS topological sort?
DSA Go
stack = append(?, stack...)
Attempts:
2 left
💡 Hint
Appending a slice to another requires the first argument to be a slice.
✗ Incorrect
To prepend node to stack, we create a new slice with node and append the existing stack after it: append([]int{node}, stack...).
🚀 Application
expertDetermine the Number of Valid Topological Orders for a Given DAG
Given the DAG below, how many valid topological orders exist?
Graph edges:
1 -> 3
2 -> 3
3 -> 4
Options:
Attempts:
2 left
💡 Hint
Consider the independent nodes and their possible orderings before the dependent nodes.
✗ Incorrect
Nodes 1 and 2 have no dependencies and can appear in any order before 3 and 4. The valid orders are [1,2,3,4] and [2,1,3,4].
