Code Quiz

BFS Graph Traversal Bug

Spot the subtle visited-tracking mistake that lets BFS enqueue the same node many times.

Codejavascript
function bfs(graph, start) {
  const visited = new Set();
  const queue = [start];
  const order = [];

  while (queue.length > 0) {
    const node = queue.shift();
    if (visited.has(node)) continue;
    visited.add(node);
    order.push(node);

    for (const neighbor of graph[node]) {
      queue.push(neighbor);
    }
  }
  return order;
}

This BFS produces the correct traversal order, but what is the subtle bug?