0
0
DSA Typescriptprogramming~20 mins

Connected Components Using BFS in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BFS Connected Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BFS Connected Components Count
What is the output of the following TypeScript code that counts connected components using BFS?
DSA Typescript
function countConnectedComponents(graph: Map<number, number[]>): number {
  const visited = new Set<number>();
  let count = 0;

  for (const node of graph.keys()) {
    if (!visited.has(node)) {
      count++;
      const queue: number[] = [node];
      visited.add(node);

      while (queue.length > 0) {
        const current = queue.shift()!;
        for (const neighbor of graph.get(current) || []) {
          if (!visited.has(neighbor)) {
            visited.add(neighbor);
            queue.push(neighbor);
          }
        }
      }
    }
  }

  return count;
}

const graph = new Map<number, number[]>([
  [1, [2]],
  [2, [1, 3]],
  [3, [2]],
  [4, [5]],
  [5, [4]],
  [6, []]
]);

console.log(countConnectedComponents(graph));
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Count how many groups of nodes are connected without crossing to other groups.
Predict Output
intermediate
2:00remaining
Output of BFS Traversal Order in Connected Components
What is the output array printed by this BFS traversal over all connected components?
DSA Typescript
function bfsTraversal(graph: Map<number, number[]>): number[] {
  const visited = new Set<number>();
  const result: number[] = [];

  for (const node of graph.keys()) {
    if (!visited.has(node)) {
      const queue: number[] = [node];
      visited.add(node);

      while (queue.length > 0) {
        const current = queue.shift()!;
        result.push(current);
        for (const neighbor of graph.get(current) || []) {
          if (!visited.has(neighbor)) {
            visited.add(neighbor);
            queue.push(neighbor);
          }
        }
      }
    }
  }

  return result;
}

const graph = new Map<number, number[]>([
  [1, [2]],
  [2, [1, 3]],
  [3, [2]],
  [4, [5]],
  [5, [4]],
  [6, []]
]);

console.log(bfsTraversal(graph));
A[1, 2, 3, 4, 5, 6]
B[1, 3, 2, 4, 5, 6]
C[1, 2, 3, 5, 4, 6]
D[6, 4, 5, 1, 2, 3]
Attempts:
2 left
💡 Hint
BFS visits neighbors level by level starting from the smallest node key.
🔧 Debug
advanced
2:00remaining
Identify the error in BFS connected components code
What error will this TypeScript code produce when run?
DSA Typescript
function countComponents(graph: Map<number, number[]>): number {
  const visited = new Set<number>();
  let count = 0;

  for (const node of graph.keys()) {
    if (!visited.has(node)) {
      count++;
      const queue: number[] = [node];

      while (queue.length > 0) {
        const current = queue.pop()!;
        visited.add(current);
        for (const neighbor of graph.get(current) || []) {
          if (!visited.has(neighbor)) {
            queue.push(neighbor);
          }
        }
      }
    }
  }

  return count;
}

const graph = new Map<number, number[]>([
  [1, [2]],
  [2, [1, 3]],
  [3, [2]],
  [4, [5]],
  [5, [4]],
  [6, []]
]);

console.log(countComponents(graph));
AThe code outputs 0 because visited nodes are never added
BThe code throws a runtime error due to empty queue access
CThe code runs correctly and outputs 3
DThe code outputs 1 because BFS is replaced by DFS using pop()
Attempts:
2 left
💡 Hint
Check how queue.pop() changes the traversal order compared to queue.shift(). Does it affect the count?
Predict Output
advanced
2:00remaining
Output of BFS with disconnected nodes and empty adjacency
What is the output of this code that counts connected components in a graph with isolated nodes?
DSA Typescript
function countComponents(graph: Map<number, number[]>): number {
  const visited = new Set<number>();
  let count = 0;

  for (const node of graph.keys()) {
    if (!visited.has(node)) {
      count++;
      const queue: number[] = [node];
      visited.add(node);

      while (queue.length > 0) {
        const current = queue.shift()!;
        for (const neighbor of graph.get(current) || []) {
          if (!visited.has(neighbor)) {
            visited.add(neighbor);
            queue.push(neighbor);
          }
        }
      }
    }
  }

  return count;
}

const graph = new Map<number, number[]>([
  [1, []],
  [2, []],
  [3, []],
  [4, []]
]);

console.log(countComponents(graph));
A1
BError: Cannot read property of undefined
C0
D4
Attempts:
2 left
💡 Hint
Each node is isolated, so each is its own component.
🧠 Conceptual
expert
2:00remaining
Minimum BFS Runs to Find All Connected Components
Given an undirected graph with N nodes and M edges, what is the minimum number of BFS runs needed to find all connected components?
AEqual to the number of nodes N
BEqual to the number of connected components
CAlways 1, since BFS covers all nodes
DEqual to the number of edges M
Attempts:
2 left
💡 Hint
Each BFS run starts from an unvisited node and covers one connected component.