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 JavaScript code performing topological sort using DFS on the given graph?
DSA Javascript
function topologicalSort(graph) {
const visited = new Set();
const stack = [];
function dfs(node) {
visited.add(node);
for (const neighbor of graph[node]) {
if (!visited.has(neighbor)) {
dfs(neighbor);
}
}
stack.push(node);
}
for (const node in graph) {
if (!visited.has(node)) {
dfs(node);
}
}
return stack.reverse();
}
const graph = {
5: [2, 0],
4: [0, 1],
2: [3],
3: [1],
0: [],
1: []
};
console.log(topologicalSort(graph));Attempts:
2 left
💡 Hint
Remember that DFS pushes nodes after visiting all neighbors, so the stack reversed gives the topological order.
✗ 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. Node 4 leads to 0 and 1. The final reversed stack is [5, 4, 2, 3, 1, 0].
❓ Predict Output
intermediateDetecting Output for a Graph with Multiple Independent Chains
What is the output of the topological sort using DFS on this graph?
DSA Javascript
function topologicalSort(graph) {
const visited = new Set();
const stack = [];
function dfs(node) {
visited.add(node);
for (const neighbor of graph[node]) {
if (!visited.has(neighbor)) {
dfs(neighbor);
}
}
stack.push(node);
}
for (const node in graph) {
if (!visited.has(node)) {
dfs(node);
}
}
return stack.reverse();
}
const graph = {
a: ['b'],
b: ['c'],
c: [],
d: ['e'],
e: []
};
console.log(topologicalSort(graph));Attempts:
2 left
💡 Hint
The DFS visits nodes in the order of object keys, so 'a' then 'b' then 'c', then 'd' then 'e'.
✗ Incorrect
DFS starts at 'a', visits 'b' and 'c', then pushes them in stack order. Then DFS starts at 'd', visits 'e', and pushes them. The reversed stack is [d, e, a, b, c].
🔧 Debug
advancedIdentify the Error in DFS Topological Sort Implementation
What error will this code produce when run, and why?
DSA Javascript
function topologicalSort(graph) {
const visited = new Set();
const stack = [];
function dfs(node) {
if (visited.has(node)) return;
for (const neighbor of graph[node]) {
dfs(neighbor);
}
visited.add(node);
stack.push(node);
}
for (const node in graph) {
dfs(node);
}
return stack.reverse();
}
const graph = {
1: [2],
2: [3],
3: []
};
console.log(topologicalSort(graph));Attempts:
2 left
💡 Hint
Check when nodes are marked visited in the DFS function.
✗ Incorrect
The code calls dfs on neighbors before marking the current node visited, so it revisits nodes repeatedly causing infinite recursion and stack overflow.
❓ Predict Output
advancedTopological Sort Output with Cycle Detection Missing
What will be the output or error of this topological sort code on a graph with a cycle?
DSA Javascript
function topologicalSort(graph) {
const visited = new Set();
const stack = [];
function dfs(node) {
visited.add(node);
for (const neighbor of graph[node]) {
if (!visited.has(neighbor)) {
dfs(neighbor);
}
}
stack.push(node);
}
for (const node in graph) {
if (!visited.has(node)) {
dfs(node);
}
}
return stack.reverse();
}
const graph = {
1: [2],
2: [3],
3: [1]
};
console.log(topologicalSort(graph));Attempts:
2 left
💡 Hint
The graph has a cycle but the code does not detect it.
✗ Incorrect
The DFS revisits nodes in the cycle endlessly because visited is only marked after recursion, causing infinite recursion and stack overflow.
🧠 Conceptual
expertUnderstanding the Role of the Stack in DFS Topological Sort
Why does the DFS-based topological sort algorithm push nodes onto a stack only after visiting all their neighbors?
Attempts:
2 left
💡 Hint
Think about the order in which nodes must appear in a topological sort.
✗ Incorrect
Pushing nodes after visiting all neighbors ensures that dependencies come before the node in the stack. Reversing the stack gives the correct topological order where each node appears after its dependencies.
