WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Coding Interview Essentials · Algorithms · question 13 of 120

Can you show me how Breadth-First Search works?

📕 Buy this interview preparation book: 120 Coding Interview Essentials questions & answers — PDF + EPUB for $5

Breadth-First Search or BFS is an algorithm for traversing or searching tree or graph data structures. Given a graph and a source vertex in the graph, BFS explores the neighbor vertices at the present depth prior to moving on to vertices at the next depth level.

Here is a more detailed explanation about how BFS works:

Breadth-First Search (BFS) starts traversal from the root node and visits nodes in a level by level manner (i.e., visiting the ones nearest to the root first). It uses a queue data structure to keep track of nodes yet to be explored.

1. Queue up the start node (root) and mark it as visited.

2. While there are nodes in the queue:

1. Dequeue the next node.

2. If this node is the goal (destination), then finish.

3. Otherwise, enqueue any neighbour nodes that haven’t been visited yet, and mark them as visited.

Here’s an example of BFS using the following undirected graph, starting from node/vertex A:

  B --- D
 /      | \
A ----  C   E

Queue: [A]

1. Visit A (Dequeue A and Enqueue its neighbours B and C into Queue)

Queue: [B, C]

2. Visit B (Dequeue B and Enqueue D (A is already visited) into Queue)

Queue: [C, D]

3. Visit C (Dequeue C and Enqueue None as node C does not have any unvisited neighbours)

Queue: [D]

4. Visit D (Dequeue D and Enqueue E (B and A are already visited) into Queue)

Queue: [E]

5. Visit E (Dequeue E and Enqueue None as it doesn’t have any unvisited neighbors).

Queue: []

Traversal Sequence: A B C D E

Just to clarify, this is just one of the possible traversal sequences because BFS can lead to different results when the nodes are enqueued in different orders, but all sequences will have nodes in the same levels grouped together.

In terms of time complexity, BFS has a time complexity of O(V+E), where V is the number of vertices and E is the number of edges, as every vertex and every edge will be explored in the worst case.

Pseudocode for BFS:

``python
BFS(graph, root):
    create empty set visited and queue Q   
    add root to visited and enqueue it into Q   
    while Q is not empty do
        v = dequeue(Q)  
        for each edge e = (v, w) in graph.adjacentEdges(v) do
            if w not in visited do
                add w to visited
                enqueue w into Q
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Coding Interview Essentials interview — then scores it.
📞 Practice Coding Interview Essentials — free 15 min
📕 Buy this interview preparation book: 120 Coding Interview Essentials questions & answers — PDF + EPUB for $5

All 120 Coding Interview Essentials questions · All topics