What is the main goal of the Number of Islands problem?
To count how many separate groups of connected '1's (land) exist in a 2D grid of '1's and '0's (land and water).
Click to reveal answer
beginner
How does Depth-First Search (DFS) help solve the Number of Islands problem?
DFS explores all connected land cells from a starting point recursively, marking them visited to avoid counting the same island multiple times.
Click to reveal answer
beginner
What is the role of Breadth-First Search (BFS) in the Number of Islands problem?
BFS uses a queue to explore all neighboring land cells level by level, marking visited cells to count each island once.
Click to reveal answer
beginner
Why do we mark cells as visited in Number of Islands solutions?
Marking cells as visited prevents revisiting the same land cell, which avoids counting the same island multiple times and infinite loops.
Click to reveal answer
intermediate
What data structures are commonly used in BFS and DFS for Number of Islands?
DFS commonly uses recursion (call stack), while BFS uses a queue to keep track of cells to visit next.
Click to reveal answer
In the Number of Islands problem, what does a '1' represent in the grid?
AObstacle
BWater
CLand
DVisited cell
✗ Incorrect
'1' represents land cells where islands can form.
Which traversal method uses a queue to explore neighbors in Number of Islands?
ABreadth-First Search
BBinary Search
CDepth-First Search
DLinear Search
✗ Incorrect
Breadth-First Search (BFS) uses a queue to explore neighbors level by level.
What happens when you find a '1' that is not visited in the grid?
AIgnore it
BStart a new island count and explore all connected land
CMark it as water
DStop the algorithm
✗ Incorrect
Finding an unvisited '1' means a new island; explore all connected land to mark it visited.
Which of these is NOT a valid neighbor direction in Number of Islands problem?
ADiagonal
BDown
CLeft
DUp
✗ Incorrect
Only vertical and horizontal neighbors (up, down, left, right) are considered, not diagonal.
Why do we mark cells as visited during traversal?
ATo sort the grid
BTo increase the island count
CTo convert land to water
DTo avoid counting the same island multiple times
✗ Incorrect
Marking visited cells prevents recounting the same island and infinite loops.
Explain how DFS works to count islands in a grid.
Think about exploring all connected parts of one island before moving to the next.
You got /4 concepts.
Describe the difference between BFS and DFS approaches in Number of Islands.
Focus on the order of visiting neighbors and data structures used.
You got /4 concepts.
Practice
(1/5)
1. What does the "Number of Islands" problem ask you to find in a 2D grid of '1's (land) and '0's (water)?
easy
A. Determine if the grid contains any water cells
B. Count how many separate groups of connected lands (islands) exist
C. Calculate the total number of '1's in the grid
D. Find the largest island size
Solution
Step 1: Understand the problem goal
The problem asks to count distinct islands, where islands are groups of connected '1's horizontally or vertically.
Step 2: Clarify what to count
We count how many such groups exist, not their sizes or total land cells.
Final Answer:
Count how many separate groups of connected lands (islands) exist -> Option B
Quick Check:
Number of islands = count of connected land groups [OK]
Hint: Islands = connected land groups, count them [OK]
Common Mistakes:
Counting total land cells instead of groups
Including diagonal connections as connected
Confusing largest island size with count
2. Which of the following Java code snippets correctly initializes a queue for BFS traversal in the Number of Islands problem?
easy
A. Queue queue = new LinkedList<>();
B. Queue queue = new Queue();
C. LinkedList queue = new Queue<>();
D. Queue<int> queue = new ArrayList<>();
Solution
Step 1: Identify correct queue initialization
In Java, Queue is an interface; LinkedList implements Queue. So, Queue queue = new LinkedList<>(); is correct.
Step 2: Check other options for errors
Queue queue = new Queue(); uses 'new Queue()' which is invalid (Queue is interface). LinkedList queue = new Queue<>(); reverses types. Queue<int> queue = new ArrayList<>(); uses ArrayList which does not implement Queue.
What is the output of a correct Number of Islands BFS or DFS implementation on this grid?
medium
A. 1
B. 2
C. 4
D. 3
Solution
Step 1: Identify islands in the grid
First island: top-left 2x2 block of '1's. Second island: single '1' at grid[2][2]. Third island: bottom-right 2 connected '1's.
Step 2: Count total islands
There are 3 separate islands in total.
Final Answer:
3 -> Option D
Quick Check:
Count distinct connected land groups = 3 [OK]
Hint: Count connected land clusters, not individual cells [OK]
Common Mistakes:
Counting diagonal connections as connected
Missing single-cell islands
Counting total '1's instead of groups
4. Consider this DFS code snippet for Number of Islands:
void dfs(char[][] grid, int r, int c) {
if (r < 0 || c < 0 || r >= grid.length || c >= grid[0].length || grid[r][c] == '0')
return;
grid[r][c] = '0';
dfs(grid, r + 1, c);
dfs(grid, r - 1, c);
dfs(grid, r, c + 1);
dfs(grid, r, c - 1);
}
What is the main bug if the grid is empty (zero rows)?
medium
A. IndexOutOfBoundsException when accessing grid[0].length
B. StackOverflowError due to infinite recursion
C. NullPointerException because grid is null
D. No bug, code works fine for empty grid
Solution
Step 1: Analyze grid access for empty grid
If grid has zero rows, grid.length == 0, so grid[0] access causes IndexOutOfBoundsException.
Step 2: Identify cause of exception
Accessing grid[0].length without checking grid length first causes error.
Final Answer:
IndexOutOfBoundsException when accessing grid[0].length -> Option A
Quick Check:
Empty grid means grid[0] invalid access [OK]
Hint: Check grid length before accessing grid[0] [OK]
Common Mistakes:
Assuming grid always has at least one row
Ignoring boundary checks before accessing grid[0]
Confusing NullPointerException with IndexOutOfBoundsException
5. You want to modify the Number of Islands BFS solution to also return the size of the largest island. Which approach correctly extends BFS to achieve this?
hard
A. Count total '1's in grid before BFS and return that as largest island size
B. Run BFS once and multiply number of islands by 2 to estimate largest size
C. During BFS, count cells visited per island and track max count across islands
D. Use DFS instead of BFS because BFS cannot count island size
Solution
Step 1: Understand BFS traversal per island
BFS visits all connected land cells of one island. Counting visited cells during BFS gives island size.
Step 2: Track largest island size
Keep a max variable updated with the largest count found after each BFS traversal.
Final Answer:
During BFS, count cells visited per island and track max count across islands -> Option C
Quick Check:
Count cells per BFS, track max size [OK]
Hint: Count cells during BFS, update max size [OK]
Common Mistakes:
Counting total land cells instead of per island
Assuming BFS cannot count sizes
Estimating size without traversal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.