Challenge - 5 Problems
DAG Shortest Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of shortest path distances after topological order processing
Given the following TypeScript code that finds shortest paths in a Directed Acyclic Graph (DAG) using topological order, what is the printed output array representing shortest distances from source node 0?
DSA Typescript
type Edge = [number, number, number]; function shortestPathDAG(n: number, edges: Edge[], src: number): number[] { const graph: Map<number, [number, number][]> = new Map(); for (let i = 0; i < n; i++) graph.set(i, []); for (const [u, v, w] of edges) { graph.get(u)!.push([v, w]); } const visited = new Array(n).fill(false); const stack: number[] = []; function dfs(node: number) { visited[node] = true; for (const [nbr] of graph.get(node)!) { if (!visited[nbr]) dfs(nbr); } stack.push(node); } for (let i = 0; i < n; i++) { if (!visited[i]) dfs(i); } const dist = new Array(n).fill(Infinity); dist[src] = 0; while (stack.length) { const node = stack.pop()!; if (dist[node] !== Infinity) { for (const [nbr, w] of graph.get(node)!) { if (dist[node] + w < dist[nbr]) { dist[nbr] = dist[node] + w; } } } } return dist; } const n = 6; const edges: Edge[] = [ [0, 1, 5], [0, 2, 3], [1, 3, 6], [1, 2, 2], [2, 4, 4], [2, 5, 2], [2, 3, 7], [3, 4, -1], [4, 5, -2] ]; console.log(shortestPathDAG(n, edges, 0));
Attempts:
2 left
💡 Hint
Trace the graph edges and update distances in topological order starting from source 0.
✗ Incorrect
The algorithm first finds a topological order of nodes. Then it initializes distances with Infinity except source 0. It relaxes edges in topological order, updating shortest distances. Negative weights are allowed but no cycles exist. The final distances array is [0, 5, 3, 10, 7, 5].
🧠 Conceptual
intermediate1:30remaining
Why does topological order help in shortest path for DAG?
Why is using topological order important when finding shortest paths in a Directed Acyclic Graph (DAG)?
Attempts:
2 left
💡 Hint
Think about how processing nodes in order affects distance relaxation.
✗ Incorrect
Topological order processes nodes so that all edges from earlier nodes to later nodes are relaxed in one pass. This avoids revisiting nodes multiple times and ensures shortest paths are found efficiently.
🔧 Debug
advanced2:00remaining
Identify the bug causing incorrect shortest path output
The following TypeScript code attempts to find shortest paths in a DAG but produces incorrect results. What is the bug?
DSA Typescript
function shortestPathBug(n: number, edges: [number, number, number][], src: number): number[] {
const graph = new Map<number, [number, number][]>();
for (let i = 0; i < n; i++) graph.set(i, []);
for (const [u, v, w] of edges) {
graph.get(u)!.push([v, w]);
}
const visited = new Array(n).fill(false);
const stack: number[] = [];
function dfs(node: number) {
visited[node] = true;
for (const [nbr] of graph.get(node)!) {
if (!visited[nbr]) dfs(nbr);
}
stack.push(node);
}
for (let i = 0; i < n; i++) {
if (!visited[i]) dfs(i);
}
const dist = new Array(n).fill(Infinity);
dist[src] = 0;
while (stack.length) {
const node = stack.pop()!;
if (dist[node] !== Infinity) {
for (const [nbr, w] of graph.get(node)!) {
if (dist[node] + w < dist[nbr]) {
dist[nbr] = dist[node] + w;
}
}
}
}
return dist;
}Attempts:
2 left
💡 Hint
Check how nodes are added to the stack during DFS and how that affects order.
✗ Incorrect
Using stack.unshift(node) adds nodes to the front, reversing the intended topological order. The algorithm expects nodes in stack in reverse topological order to pop correctly. This causes incorrect distance updates.
📝 Syntax
advanced1:30remaining
Identify the syntax error in the shortest path code snippet
Which option contains a syntax error preventing the shortest path code from compiling?
DSA Typescript
const graph = new Map<number, [number, number][]>() for (let i = 0; i < n; i++) { graph.set(i, []) } for (const [u, v, w] of edges) { graph.get(u)!.push([v, w]) } const dist = new Array(n).fill(Infinity) dist[src] = 0 while (stack.length) { const node = stack.pop()! if (dist[node] !== Infinity) { for (const [nbr, w] of graph.get(node)!) { if (dist[node] + w < dist[nbr]) { dist[nbr] = dist[node] + w } } } }
Attempts:
2 left
💡 Hint
Look for missing punctuation between statements.
✗ Incorrect
In TypeScript, statements must be separated by semicolons or newlines. Option A misses a semicolon or newline between two statements, causing a syntax error.
🚀 Application
expert2:00remaining
Calculate number of nodes reachable with finite shortest path
After running shortest path in a DAG from source 0, how many nodes have a finite shortest distance (not Infinity) given the edges below?
DSA Typescript
const n = 7; const edges: [number, number, number][] = [ [0, 1, 2], [0, 2, 4], [1, 3, 7], [2, 4, 1], [4, 3, 2], [5, 6, 3] ]; // Run shortest path from source 0 using topological order // Count nodes with dist[node] !== Infinity
Attempts:
2 left
💡 Hint
Nodes 5 and 6 are disconnected from source 0.
✗ Incorrect
Nodes reachable from 0 are 0,1,2,3,4 with finite distances. Nodes 5 and 6 are disconnected, so their distances remain Infinity. Total reachable nodes with finite distance: 5.