0
0
DSA Typescriptprogramming~20 mins

Dijkstra's Algorithm Single Source Shortest Path in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dijkstra Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Dijkstra's Algorithm on a Simple Graph

What is the shortest distance array output after running Dijkstra's algorithm from node 0?

DSA Typescript
const graph = [
  [{node:1, weight:4}, {node:2, weight:1}],
  [{node:3, weight:1}],
  [{node:1, weight:2}, {node:3, weight:5}],
  []
];

function dijkstra(graph: {node:number, weight:number}[][], start: number): number[] {
  const dist = Array(graph.length).fill(Infinity);
  dist[start] = 0;
  const visited = new Set<number>();
  while (visited.size < graph.length) {
    let u = -1;
    let minDist = Infinity;
    for (let i = 0; i < dist.length; i++) {
      if (!visited.has(i) && dist[i] < minDist) {
        minDist = dist[i];
        u = i;
      }
    }
    if (u === -1) break;
    visited.add(u);
    for (const edge of graph[u]) {
      if (dist[u] + edge.weight < dist[edge.node]) {
        dist[edge.node] = dist[u] + edge.weight;
      }
    }
  }
  return dist;
}

console.log(dijkstra(graph, 0));
A[0, 3, 2, 5]
B[0, 4, 1, 6]
C[0, 2, 1, 3]
D[0, 3, 1, 4]
Attempts:
2 left
💡 Hint

Trace the shortest path updates step by step from node 0.

🧠 Conceptual
intermediate
1:30remaining
Understanding Dijkstra's Algorithm Behavior

Which statement correctly describes Dijkstra's algorithm behavior on graphs with negative edge weights?

AIt always finds the shortest path even with negative edges.
BIt may fail to find the correct shortest path if negative edges exist.
CIt ignores negative edges and treats them as zero weight.
DIt runs faster on graphs with negative edges.
Attempts:
2 left
💡 Hint

Think about how negative weights affect path cost updates.

🔧 Debug
advanced
2:00remaining
Identify the Bug in Dijkstra's Implementation

What error will this Dijkstra's algorithm code produce when run?

DSA Typescript
function dijkstraBug(graph: {node:number, weight:number}[][], start: number): number[] {
  const dist = Array(graph.length).fill(Infinity);
  dist[start] = 0;
  const visited = new Set<number>();
  while (visited.size <= graph.length) {
    let u = -1;
    let minDist = Infinity;
    for (let i = 0; i < dist.length; i++) {
      if (!visited.has(i) && dist[i] < minDist) {
        minDist = dist[i];
        u = i;
      }
    }
    if (u === -1) break;
    visited.add(u);
    for (const edge of graph[u]) {
      if (dist[u] + edge.weight < dist[edge.node]) {
        dist[edge.node] = dist[u] + edge.weight;
      }
    }
  }
  return dist;
}

const graph = [
  [{node:1, weight:7}],
  [{node:2, weight:5}],
  []
];
console.log(dijkstraBug(graph, 0));
AInfinite loop causing program to hang
BReturns correct shortest distances [0,7,12]
CThrows TypeError due to undefined node access
DReturns array with all Infinity values
Attempts:
2 left
💡 Hint

Check the loop condition carefully.

🚀 Application
advanced
1:30remaining
Choosing Data Structures for Efficient Dijkstra

Which data structure choice will optimize Dijkstra's algorithm for a graph with many nodes and edges?

AUse an adjacency list and a min-priority queue (min-heap) for selecting next node
BUse an adjacency matrix and a simple array for distances
CUse a linked list for graph edges and a stack for node selection
DUse a hash map for edges and a queue for node selection
Attempts:
2 left
💡 Hint

Think about how to quickly find the next closest node.

Predict Output
expert
2:30remaining
Output of Dijkstra's Algorithm with Multiple Paths

What is the shortest distance array output after running Dijkstra's algorithm from node 0 on this graph?

DSA Typescript
const graph = [
  [{node:1, weight:10}, {node:2, weight:3}],
  [{node:2, weight:1}, {node:3, weight:2}],
  [{node:1, weight:4}, {node:3, weight:8}, {node:4, weight:2}],
  [{node:4, weight:7}],
  [{node:3, weight:9}]
];

function dijkstra(graph: {node:number, weight:number}[][], start: number): number[] {
  const dist = Array(graph.length).fill(Infinity);
  dist[start] = 0;
  const visited = new Set<number>();
  while (visited.size < graph.length) {
    let u = -1;
    let minDist = Infinity;
    for (let i = 0; i < dist.length; i++) {
      if (!visited.has(i) && dist[i] < minDist) {
        minDist = dist[i];
        u = i;
      }
    }
    if (u === -1) break;
    visited.add(u);
    for (const edge of graph[u]) {
      if (dist[u] + edge.weight < dist[edge.node]) {
        dist[edge.node] = dist[u] + edge.weight;
      }
    }
  }
  return dist;
}

console.log(dijkstra(graph, 0));
A[0, 7, 3, 5, 5]
B[0, 7, 3, 9, 5]
C[0, 10, 3, 5, 5]
D[0, 10, 3, 8, 2]
Attempts:
2 left
💡 Hint

Consider all possible paths and update distances carefully.