Problem, as an interviewer would state it:
"Implement a Breadth-First Search (bfs(graph, start)) over a graph represented as an adjacency list — visiting nodes level by level from start, and returning the order they were visited in."
Examples:
bfs({A:["B","C"], B:["A","D"], C:["A","D"], D:["B","C"]}, "A");
// ["A", "B", "C", "D"]
Clarifying questions expected:
- Is the graph represented as an adjacency list (an object/map of node → array of neighbors), and is it undirected (edges implied both ways) or directed?
- If the graph is genuinely DISCONNECTED, should the traversal only visit
start's own connected component, or attempt to visit every node in the whole graph regardless of reachability? - Does the graph need to be correctly handled if it contains a real CYCLE, without infinite-looping?
Code / implementation expected: Yes — real, direct proof of level-by-level visit order, correct behavior on a real disconnected graph, and correct termination on a real graph containing a cycle.