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
intermediate
How does Depth-First Search (DFS) help solve the Number of Islands problem?
DFS explores all connected land cells from a starting point by going deep into neighbors recursively, marking visited cells to avoid counting the same island multiple times.
Click to reveal answer
intermediate
What is the role of Breadth-First Search (BFS) in the Number of Islands problem?
BFS explores all neighbors level by level from a starting land cell using a queue, marking visited cells to cover the entire island before moving to the next.
Click to reveal answer
beginner
Why do we mark cells as visited in Number of Islands solutions?
Marking cells as visited prevents counting the same land cell multiple times and avoids infinite loops during traversal.
Click to reveal answer
intermediate
What data structures are commonly used in BFS and DFS for Number of Islands?
DFS uses recursion (call stack) or an explicit stack; 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?
ABoundary
BWater
CVisited cell
DLand
✗ Incorrect
In the grid, '1' represents land, and '0' represents water.
Which traversal method uses a queue to explore neighbors in the Number of Islands problem?
ADepth-First Search (DFS)
BBreadth-First Search (BFS)
CBinary Search
DMerge Sort
✗ Incorrect
BFS uses a queue to explore neighbors level by level.
What is the main reason to mark cells as visited during traversal?
ATo avoid revisiting and infinite loops
BTo count water cells
CTo sort the grid
DTo change land to water
✗ Incorrect
Marking visited cells prevents revisiting the same cell and infinite loops.
Which of these is NOT a valid neighbor direction in Number of Islands traversal?
ADiagonal
BDown
CLeft
DUp
✗ Incorrect
Only up, down, left, and right neighbors are considered; diagonals are not.
If the grid has no '1's, what is the number of islands?
ANumber of rows
B1
C0
DNumber of columns
✗ Incorrect
No land means zero islands.
Explain how DFS works to find the number of islands in a grid.
Think about going deep into neighbors before backtracking.
You got /4 concepts.
Describe the steps BFS takes to count islands in the grid.
Imagine spreading out from a point in waves.
You got /4 concepts.
Practice
(1/5)
1. What does the "Number of Islands" problem ask you to find in a grid of '1's and '0's?
easy
A. Count how many groups of connected '1's (islands) are in the grid
B. Find the largest island size in the grid
C. Calculate the total number of '0's in the grid
D. Replace all '1's with '0's in the grid
Solution
Step 1: Understand the grid representation
The grid contains '1's representing land and '0's representing water.
Step 2: Identify what an island means
An island is a group of connected '1's horizontally or vertically.
Final Answer:
Count how many groups of connected '1's (islands) are in the grid -> Option A
Quick Check:
Number of islands = count of connected '1' groups [OK]
Hint: Islands = connected '1's groups, count them [OK]
2. Which of the following is the correct way to start a DFS function in JavaScript for the Number of Islands problem?
easy
A. function dfs(grid, r, c) { if (r < 0 || c < 0) return; }
B. function dfs(grid, r, c) { if (r <= 0 || c <= 0 || r > grid.length || c > grid[0].length) return; }
C. function dfs(grid, r, c) { if (r > grid.length || c > grid[0].length) return; }
D. function dfs(grid, r, c) { if (r < 0 || c < 0 || r >= grid.length || c >= grid[0].length) return; }
Solution
Step 1: Check boundary conditions for DFS
We must stop DFS if row or column is outside grid bounds.
Step 2: Write correct boundary check
Row or column less than 0 or greater or equal to grid size means out of bounds.
Final Answer:
function dfs(grid, r, c) { if (r < 0 || c < 0 || r >= grid.length || c >= grid[0].length) return; } -> Option D
Quick Check:
Boundary check includes all invalid indices [OK]
Hint: Check all out-of-bound indices with < 0 or >= length [OK]
Common Mistakes:
Using > instead of >= for boundary check
Missing some boundary conditions
Using <= 0 which excludes valid index 0
3. What will be the output of the following code snippet for the grid below?
const grid = [
['1','1','0','0'],
['1','0','0','1'],
['0','0','1','1'],
['0','0','0','0']
];
function numIslands(grid) {
let count = 0;
const dfs = (r, c) => {
if (r < 0 || c < 0 || r >= grid.length || c >= grid[0].length || grid[r][c] === '0') return;
grid[r][c] = '0';
dfs(r + 1, c);
dfs(r - 1, c);
dfs(r, c + 1);
dfs(r, c - 1);
};
for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[0].length; c++) {
if (grid[r][c] === '1') {
dfs(r, c);
count++;
}
}
}
return count;
}
console.log(numIslands(grid));
medium
A. 1
B. 3
C. 2
D. 4
Solution
Step 1: Identify islands in the grid
Islands are groups of connected '1's. Here, islands are: top-left (3 cells: (0,0),(0,1),(1,0)), single cell at (1,3), and bottom cluster (2 cells: (2,2),(2,3)). Total islands = 3.
Step 2: Count islands using DFS
DFS marks visited '1's as '0'. After visiting each island, count increments. Total islands counted = 3.
Final Answer:
3 -> Option B
Quick Check:
Count connected '1' groups = 3 [OK]
Hint: Count connected '1' groups by marking visited [OK]
Common Mistakes:
Counting diagonal connections as islands
Not marking visited cells, causing overcount
Misreading grid layout
4. The following BFS code for Number of Islands has a bug. What is the bug?
function bfs(grid, r, c) {
const queue = [[r, c]];
while (queue.length > 0) {
const [row, col] = queue.shift();
if (row < 0 || col < 0 || row >= grid.length || col >= grid[0].length || grid[row][col] === '0') continue;
grid[row][col] = '0';
queue.push([row + 1, col]);
queue.push([row - 1, col]);
queue.push([row, col + 1]);
queue.push([row, col - 1]);
}
}
medium
A. It does not check if a cell is already visited before pushing neighbors
B. It marks visited cells after adding neighbors to queue
C. It uses queue.shift() which is inefficient for large grids
D. It uses recursion instead of iteration
Solution
Step 1: Analyze neighbor addition in BFS
The code adds neighbors to the queue without checking if they are visited or water.
Step 2: Identify consequence of missing check
This causes multiple visits to the same cell, increasing runtime and possible infinite loops.
Final Answer:
It does not check if a cell is already visited before pushing neighbors -> Option A
Quick Check:
Check before enqueueing neighbors to avoid repeats [OK]
Hint: Check visited before enqueueing neighbors in BFS [OK]
Common Mistakes:
Thinking queue.shift() inefficiency is the bug here
Marking visited after enqueue instead of before
Confusing recursion with iteration
5. Given a grid where diagonal connections also count as connected land, how would you modify the DFS to count islands correctly?
hard
A. Ignore diagonal neighbors and count only vertical connections
B. Only check horizontal neighbors (r,c+1) and (r,c-1)
C. Add diagonal neighbors (r+1,c+1), (r+1,c-1), (r-1,c+1), (r-1,c-1) to DFS calls
D. Use BFS instead of DFS without changing neighbors
Solution
Step 1: Understand connectivity rules
Diagonal connections mean neighbors include diagonals, not just up/down/left/right.
Step 2: Modify DFS to include diagonal neighbors
Add DFS calls for (r+1,c+1), (r+1,c-1), (r-1,c+1), (r-1,c-1) along with existing neighbors.
Final Answer:
Add diagonal neighbors (r+1,c+1), (r+1,c-1), (r-1,c+1), (r-1,c-1) to DFS calls -> Option C
Quick Check:
Include diagonal neighbors for diagonal connectivity [OK]
Hint: Add diagonal neighbors to DFS for diagonal island counting [OK]
Common Mistakes:
Ignoring diagonal neighbors when required
Switching BFS/DFS without changing neighbor logic
Counting only horizontal or vertical neighbors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.