BFS stands for Breadth First Search. It is a method to explore all nodes of a graph level by level, starting from a chosen node.
Click to reveal answer
beginner
In BFS, which data structure is primarily used to keep track of nodes to visit next?
A queue is used in BFS to keep track of nodes to visit next. It ensures nodes are explored in the order they were discovered.
Click to reveal answer
intermediate
What is the main difference between BFS and DFS in graph traversal?
BFS explores nodes level by level using a queue, while DFS explores as far as possible along each 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), where V is the number of vertices and E is the number of edges, because each vertex and edge is processed 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 traversal of a graph?
AVisit all neighbors of the starting node
BPush all nodes into a stack
CMark all nodes as visited
DStart from a chosen node and enqueue it
✗ Incorrect
BFS starts by choosing a node and adding it to the queue to begin traversal.
How does BFS ensure it does not visit the same node multiple times?
ABy marking nodes as visited
BBy using recursion
CBy sorting nodes
DBy using a stack
✗ Incorrect
BFS marks nodes as visited to avoid revisiting them.
Which of these is a typical use case of BFS?
AFinding shortest path in an unweighted graph
BSorting numbers
CDepth-first traversal
DCalculating factorial
✗ Incorrect
BFS is used to find the shortest path in unweighted graphs because it explores neighbors level by level.
What is the time complexity of BFS on a graph?
AO(V^2)
BO(E^2)
CO(V + E)
DO(log V)
✗ Incorrect
BFS runs in O(V + E) time, processing each vertex and edge once.
Explain how BFS works step-by-step on a graph starting from a given node.
Think about visiting nodes level by level using a queue.
You got /6 concepts.
Describe why BFS is suitable for finding the shortest path in an unweighted graph.
Consider how BFS visits nodes in order of their distance from the start.
You got /5 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 stack to explore nodes deeply first
B. A hash map to store visited nodes only
C. A queue to explore nodes level by level
D. A priority queue to explore nodes by weight
Solution
Step 1: Understand BFS traversal method
BFS explores nodes level by level, so it needs to remember nodes in the order they are discovered.
Step 2: Identify the data structure for order
A queue follows First-In-First-Out (FIFO), perfect for level order traversal in BFS.
Final Answer:
A queue to explore nodes level by level -> Option C
Quick Check:
BFS uses queue = A [OK]
Hint: BFS uses queue, DFS uses stack [OK]
Common Mistakes:
Confusing BFS with DFS and choosing stack
Thinking BFS uses priority queue
Ignoring the order of node visits
2. Which of the following is the correct syntax to enqueue a node `v` into a queue named `queue` in JavaScript?
easy
A. queue.push(v);
B. queue.enqueue(v);
C. queue.add(v);
D. queue.insert(v);
Solution
Step 1: Recall JavaScript array methods for queue
JavaScript arrays use `push()` to add elements at the end, simulating enqueue.
Step 2: Verify other options
Methods like `enqueue()`, `add()`, or `insert()` are not standard array methods in JavaScript.
Final Answer:
queue.push(v); -> Option A
Quick Check:
Enqueue in JS array = push() [OK]
Hint: Use push() to add at queue's end in JS [OK]
Common Mistakes:
Using non-existent enqueue() method
Confusing push() with pop()
Trying to use add() or insert() which don't exist
3. Consider the following graph represented as adjacency list:
A. Visited nodes are not marked before enqueueing, causing duplicates
B. Visited set is never updated, causing infinite loop
C. Visited nodes are marked after enqueueing, causing duplicates
D. Queue is not initialized properly
Solution
Step 1: Check order of visited marking
Visited nodes are marked after enqueueing neighbors, which can cause duplicates if neighbors appear multiple times.
Step 2: Correct approach
Mark nodes as visited before enqueueing to avoid adding duplicates to the queue.
Final Answer:
Visited nodes are not marked before enqueueing, causing duplicates -> Option A
Quick Check:
Mark visited before enqueue = no duplicates [OK]
Hint: Mark visited before enqueue to avoid duplicates [OK]
Common Mistakes:
Marking visited after enqueue
Not marking visited at all
Initializing queue incorrectly
5. Given a graph with nodes and edges, how can BFS be adapted to find the shortest path length from a start node to a target node? Choose the best approach.
hard
A. Use a stack to explore nodes and update shortest distance dynamically
B. Use DFS and record path length when target is found
C. Use BFS but do not track visited nodes to find all paths
D. Use BFS with a queue and track distance for each node when first visited
Solution
Step 1: Understand BFS property for shortest path
BFS explores nodes level by level, so the first time we reach the target node is the shortest path.
Step 2: Track distance during BFS
Maintain a distance map or array, updating distance when a node is first visited to record shortest path length.
Final Answer:
Use BFS with a queue and track distance for each node when first visited -> Option D
Quick Check:
BFS + distance tracking = shortest path [OK]
Hint: Track distance on first visit in BFS for shortest path [OK]
Common Mistakes:
Using DFS which may not find shortest path
Not tracking visited nodes causing infinite loops
Using stack instead of queue for BFS
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.