BFS stands for Breadth First Search. It is a way to explore all nodes in a graph level by level, starting from a chosen node.
Click to reveal answer
beginner
In BFS, which data structure is mainly used to keep track of nodes to visit next?
A queue is used in BFS to keep track of nodes to visit next. It helps explore nodes in the order they were discovered.
Click to reveal answer
intermediate
What is the main difference between BFS and DFS (Depth First Search)?
BFS explores nodes level by level using a queue, while DFS explores as far as possible along one branch before backtracking, using a stack or recursion.
Click to reveal answer
beginner
Why do we mark nodes as visited in BFS?
Marking nodes as visited prevents revisiting the same node multiple times, which avoids infinite loops and repeated work.
Click to reveal answer
intermediate
What is the time complexity of BFS on a graph with V vertices and E edges?
The time complexity of BFS is O(V + E), because it visits every vertex and edge at most once.
Click to reveal answer
Which data structure does BFS use to keep track of nodes to visit?
ATree
BQueue
CHash Map
DStack
✗ Incorrect
BFS uses a queue to explore nodes in the order they are discovered.
What is the first step in BFS starting from a node?
AAdd all neighbors to the queue
BRemove the starting node from the graph
CMark all nodes as visited
DAdd the starting node to the queue and mark it visited
✗ Incorrect
BFS starts by adding the starting node to the queue and marking it visited.
What happens when BFS visits a node's neighbor that is already visited?
AIt skips adding it to the queue again
BIt adds it again to the queue
CIt removes the neighbor from the graph
DIt restarts the BFS
✗ Incorrect
BFS skips neighbors already visited to avoid loops and repeated visits.
Which of these is NOT true about BFS?
AIt uses a stack to keep track of nodes
BIt can find the shortest path in an unweighted graph
CIt explores nodes level by level
DIt marks nodes as visited
✗ Incorrect
BFS uses a queue, not a stack, to keep track of nodes.
What is the order of visiting nodes in BFS starting from node 1 in this graph? 1 connected to 2 and 3, 2 connected to 4
A1, 4, 2, 3
B1, 3, 2, 4
C1, 2, 3, 4
D4, 2, 3, 1
✗ Incorrect
BFS visits neighbors level by level: start at 1, then 2 and 3, then 4.
Explain how BFS works step-by-step on a simple graph starting from a node.
Think about how you explore friends in layers starting from one person.
You got /5 concepts.
Describe why BFS is useful for finding the shortest path in an unweighted graph.
Imagine walking through rooms one step at a time.
You got /4 concepts.
Practice
(1/5)
1. What does BFS (Breadth First Search) primarily use to keep track of nodes to visit next in a graph?
easy
A. A priority queue to select nodes by weight
B. A stack to process nodes in depth order
C. A hash map to store visited nodes only
D. A queue to process nodes level by level
Solution
Step 1: Understand BFS traversal method
BFS explores nodes level by level, so it needs a structure that processes nodes in the order they are discovered.
Step 2: Identify the data structure used
A queue follows First-In-First-Out (FIFO) order, which matches BFS's level order traversal.
Final Answer:
A queue to process nodes level by level -> Option D
Quick Check:
BFS uses a queue = A [OK]
Hint: BFS uses FIFO order, so it needs a queue [OK]
Common Mistakes:
Confusing BFS with DFS which uses a stack
Thinking BFS uses a priority queue
Assuming BFS only uses a visited set without a queue
2. Which of the following is the correct TypeScript syntax to initialize a queue for BFS using an array?
easy
A. const queue = new Queue<number>();
B. const queue: number[] = [];
C. let queue = {};
D. var queue = new ArrayQueue();
Solution
Step 1: Recognize TypeScript array initialization
To create a queue using an array, declare an array with type annotation and initialize it empty.
Step 2: Check each option
const queue: number[] = []; correctly declares an empty number array. const queue = new Queue<number>(); uses a class Queue which is not built-in. let queue = {}; initializes an object, not a queue. var queue = new ArrayQueue(); uses a non-existent ArrayQueue.
Final Answer:
const queue: number[] = []; -> Option B
Quick Check:
Array queue syntax = A [OK]
Hint: Use typed empty array for queue in TypeScript [OK]
Common Mistakes:
Using undefined classes like Queue without import
Initializing queue as an object {} instead of array
Using var instead of const or let (less preferred)
3. Given the graph adjacency list below and BFS starting from node 0, what is the order of nodes visited?