Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Connected Components Using BFS in DSA Javascript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the visited set before BFS starts.

DSA Javascript
const visited = new [1]();
Drag options to blanks, or click blank then click option'
AObject
BMap
CArray
DSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using an Array instead of a Set causes slower lookups.
Using an Object or Map is unnecessary for just tracking visited nodes.
2fill in blank
medium

Complete the code to add the starting node to the queue in BFS.

DSA Javascript
queue.push([1]);
Drag options to blanks, or click blank then click option'
AstartNode
Bgraph
Cvisited
Dneighbors
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the whole graph or visited set instead of a single node.
Adding neighbors before processing the start node.
3fill in blank
hard

Fix the error in the BFS loop condition to continue while queue is not empty.

DSA Javascript
while (queue.[1] > 0) {
Drag options to blanks, or click blank then click option'
Asize
Blength
Ccount
Dlengths
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' which is for Sets or Maps, not arrays.
Using incorrect property names like 'count' or 'lengths'.
4fill in blank
hard

Fill both blanks to check if a neighbor is unvisited and add it to the queue.

DSA Javascript
if (!visited.[1](neighbor)) {
  visited.[2](neighbor);
  queue.push(neighbor);
}
Drag options to blanks, or click blank then click option'
Ahas
Badd
Cpush
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' on a Set instead of 'add'.
Using 'includes' which is for arrays, not Sets.
5fill in blank
hard

Fill all three blanks to build the connected components list using BFS.

DSA Javascript
for (const node of Object.keys(graph)) {
  if (!visited.[1](node)) {
    const component = [];
    const queue = [node];
    visited.[2](node);
    while (queue.[3] > 0) {
      const current = queue.shift();
      component.push(current);
      for (const neighbor of graph[current]) {
        if (!visited.has(neighbor)) {
          visited.add(neighbor);
          queue.push(neighbor);
        }
      }
    }
    components.push(component);
  }
}
Drag options to blanks, or click blank then click option'
Ahas
Badd
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' for array length.
Using 'push' instead of 'add' for Sets.

Practice

(1/5)
1. What does the BFS algorithm help find in an undirected graph when used for connected components?
easy
A. All nodes reachable from a starting node forming one connected component
B. The shortest path between two nodes only
C. The maximum degree of any node in the graph
D. The total number of edges in the graph

Solution

  1. Step 1: Understand BFS purpose in connected components

    BFS explores all nodes reachable from a starting node, marking them as part of the same connected component.
  2. Step 2: Differentiate BFS from other graph properties

    BFS does not directly find shortest paths between arbitrary nodes, maximum degree, or edge count in this context.
  3. Final Answer:

    All nodes reachable from a starting node forming one connected component -> Option A
  4. Quick Check:

    BFS finds connected components = D [OK]
Hint: BFS visits all nodes connected to start node [OK]
Common Mistakes:
  • Confusing BFS with shortest path only
  • Thinking BFS counts edges or degrees
  • Mixing BFS with DFS concepts
2. Which of the following is the correct way to initialize a queue for BFS in JavaScript?
easy
A. const queue = new Set(); queue.add(startNode);
B. const queue = []; queue.push(startNode);
C. const queue = {}; queue.enqueue(startNode);
D. const queue = null; queue.push(startNode);

Solution

  1. Step 1: Identify correct queue initialization

    In JavaScript, a queue for BFS is commonly implemented using an array with push() to add elements.
  2. Step 2: Check other options for errors

    Set does not have push(), object {} has no enqueue method, and null cannot be used as a queue.
  3. Final Answer:

    const queue = []; queue.push(startNode); -> Option B
  4. Quick Check:

    Array with push() for queue = B [OK]
Hint: Use array with push/pop or shift for BFS queue [OK]
Common Mistakes:
  • Using Set instead of array for queue
  • Using object without queue methods
  • Initializing queue as null
3. What will be the output of the following code snippet that finds connected components using BFS?
const graph = {
  0: [1],
  1: [0, 2],
  2: [1],
  3: [4],
  4: [3]
};

function bfs(start, visited) {
  const queue = [start];
  visited[start] = true;
  const component = [];
  while (queue.length) {
    const node = queue.shift();
    component.push(node);
    for (const neighbor of graph[node]) {
      if (!visited[neighbor]) {
        visited[neighbor] = true;
        queue.push(neighbor);
      }
    }
  }
  return component;
}

const visited = {};
const components = [];
for (const node in graph) {
  if (!visited[node]) {
    components.push(bfs(node, visited));
  }
}
console.log(components);
medium
A. [[0,1,2],[3,4]]
B. [[0,1],[2,3,4]]
C. [[0,1,2,3,4]]
D. [[1,0,2],[4,3]]

Solution

  1. Step 1: Trace BFS from node 0

    BFS visits 0, then 1, then 2 forming component [0,1,2].
  2. Step 2: Trace BFS from node 3

    Next unvisited node is 3, BFS visits 3 and 4 forming component [3,4].
  3. Final Answer:

    [[0,1,2],[3,4]] -> Option A
  4. Quick Check:

    Two connected components found = A [OK]
Hint: BFS groups connected nodes in order visited [OK]
Common Mistakes:
  • Assuming all nodes in one component
  • Mixing order of nodes in components
  • Ignoring visited tracking
4. Identify the bug in this BFS connected components code snippet:
function bfs(start, visited, graph) {
  const queue = [start];
  visited[start] = true;
  const component = [];
  while (queue.length > 0) {
    const node = queue.pop();
    component.push(node);
    for (const neighbor of graph[node]) {
      if (!visited[neighbor]) {
        visited[neighbor] = true;
        queue.push(neighbor);
      }
    }
  }
  return component;
}
medium
A. For loop syntax is incorrect
B. Not marking start node as visited
C. Using queue.pop() instead of queue.shift() causes wrong BFS order
D. queue.push(neighbor) should be queue.unshift(neighbor)

Solution

  1. Step 1: Check queue operation for BFS

    BFS requires removing from front (queue.shift()), but code uses pop() which removes from end, making it DFS behavior.
  2. Step 2: Verify other parts

    Start node is marked visited, for loop syntax is correct, and push() is correct to add neighbors at end.
  3. Final Answer:

    Using queue.pop() instead of queue.shift() causes wrong BFS order -> Option C
  4. Quick Check:

    pop() breaks BFS order = A [OK]
Hint: Use shift() to dequeue in BFS, pop() is for DFS [OK]
Common Mistakes:
  • Confusing pop() with shift() in queue
  • Not marking visited nodes
  • Incorrect loop syntax
5. Given a graph represented as an adjacency list, how would you modify the BFS connected components algorithm to count the number of connected components without storing the nodes in each component?
hard
A. Use DFS instead of BFS to count components
B. Store all nodes in a global array and count unique nodes after BFS
C. Count edges visited during BFS to find components
D. Increment a counter each time BFS starts from an unvisited node, without collecting nodes

Solution

  1. Step 1: Understand BFS role in counting components

    Each BFS call from an unvisited node explores one connected component fully.
  2. Step 2: Modify BFS to count components

    Instead of storing nodes, just increment a counter when BFS starts from an unvisited node.
  3. Final Answer:

    Increment a counter each time BFS starts from an unvisited node, without collecting nodes -> Option D
  4. Quick Check:

    Count BFS starts = connected components [OK]
Hint: Count BFS calls from unvisited nodes for components [OK]
Common Mistakes:
  • Storing all nodes unnecessarily
  • Confusing edge count with component count
  • Switching BFS to DFS without reason
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard