0
0
DSA Typescriptprogramming~20 mins

Shortest Path in Unweighted Graph Using BFS in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shortest Path BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BFS shortest path distances
What is the output of the following TypeScript code that finds shortest path distances from node 0 in an unweighted graph?
DSA Typescript
function bfsShortestPath(graph: number[][], start: number): number[] {
  const distances = Array(graph.length).fill(-1);
  const queue: number[] = [];
  distances[start] = 0;
  queue.push(start);
  while (queue.length > 0) {
    const node = queue.shift()!;
    for (const neighbor of graph[node]) {
      if (distances[neighbor] === -1) {
        distances[neighbor] = distances[node] + 1;
        queue.push(neighbor);
      }
    }
  }
  return distances;
}

const graph = [
  [1, 2],
  [0, 3],
  [0, 3],
  [1, 2, 4],
  [3]
];
console.log(bfsShortestPath(graph, 0));
A[1, 0, 0, 1, 2]
B[0, 1, 1, 1, 2]
C[0, 2, 2, 3, 4]
D[0, 1, 1, 2, 3]
Attempts:
2 left
💡 Hint
Think about how BFS explores neighbors level by level starting from the start node.
🧠 Conceptual
intermediate
1:30remaining
Understanding BFS queue behavior
In BFS for shortest path in an unweighted graph, what is the main reason for using a queue data structure?
ATo explore nodes in the order they are discovered, ensuring shortest path layers are processed first
BTo store nodes in sorted order by their values
CTo keep track of visited nodes using a stack-like behavior
DTo randomly select the next node to explore
Attempts:
2 left
💡 Hint
Think about how BFS explores nodes level by level.
🔧 Debug
advanced
2:00remaining
Identify the bug in BFS shortest path code
What error will this TypeScript BFS shortest path code produce when run, and why?
DSA Typescript
function bfsShortestPath(graph: number[][], start: number): number[] {
  const distances = Array(graph.length).fill(-1);
  const queue: number[] = [];
  distances[start] = 0;
  queue.push(start);
  while (queue.length > 0) {
    const node = queue.pop()!;
    for (const neighbor of graph[node]) {
      if (distances[neighbor] === -1) {
        distances[neighbor] = distances[node] + 1;
        queue.push(neighbor);
      }
    }
  }
  return distances;
}
ATypeError because distances array is not initialized properly
BSyntaxError due to missing semicolon after queue.push(start)
CThe code runs but returns incorrect distances because it uses pop() instead of shift() causing DFS behavior
DRuntime error because graph[node] is undefined
Attempts:
2 left
💡 Hint
Check how the queue is used to remove elements.
🚀 Application
advanced
2:30remaining
Shortest path reconstruction from BFS distances
Given BFS distances from start node 0, how can you reconstruct the actual shortest path to node 4 in the graph below? Graph adjacency list: 0: [1, 2] 1: [0, 3] 2: [0, 3] 3: [1, 2, 4] 4: [3] Distances from 0: [0, 1, 1, 2, 3]
AStart from node 4 and move to any neighbor with distance one less until reaching 0, collecting nodes in reverse
BStart from node 0 and move to neighbors with larger distance until reaching 4
CRandomly pick neighbors until node 4 is reached
DUse a stack to store nodes visited during BFS and pop until node 4 is found
Attempts:
2 left
💡 Hint
Shortest path can be traced backward using distances.
🧠 Conceptual
expert
1:30remaining
Why BFS guarantees shortest path in unweighted graphs
Why does BFS always find the shortest path in an unweighted graph?
ABecause BFS uses a stack to explore the deepest nodes first
BBecause BFS explores nodes in order of increasing distance from the start node using a queue
CBecause BFS sorts neighbors by their node values before visiting
DBecause BFS uses recursion to explore all paths simultaneously
Attempts:
2 left
💡 Hint
Think about the order nodes are visited in BFS.