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 cell by going deep into neighbors recursively, marking visited cells 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 explores all connected land cells level by level using a queue, 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?
To avoid counting the same land cell multiple times and to prevent 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?
ALand
BWater
CVisited cell
DObstacle
✗ Incorrect
'1' represents land cells where islands can form.
Which traversal method uses a queue to explore neighbors in Number of Islands?
ADepth-First Search (DFS)
BBreadth-First Search (BFS)
CBinary Search
DLinear Search
✗ Incorrect
BFS uses a queue to explore neighbors level by level.
What happens when you find a '1' in the grid during traversal?
AStop the algorithm
BIgnore it
CStart a new island count and explore all connected land cells
DMark it as water
✗ Incorrect
Finding '1' means a new island or part of one; we explore connected land to count it once.
Why do we mark cells as visited during BFS or DFS?
ATo sort the grid
BTo increase the island count
CTo convert land to water
DTo avoid revisiting and recounting the same island
✗ Incorrect
Marking visited prevents counting the same land multiple times.
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, not diagonals.
Explain how DFS can be used to count the number of islands in a grid.
Think about how you would explore all connected parts of an island deeply before moving on.
You got /4 concepts.
Describe the difference between BFS and DFS approaches in the Number of Islands problem.
Consider how each method explores neighbors differently.
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 all '0's in the grid
B. Find the largest island size
C. Count how many groups of connected '1's (islands) exist
D. Calculate the perimeter of all islands
Solution
Step 1: Understand the grid representation
The grid contains '1's representing land and '0's representing water.
Step 2: Identify what the problem asks
It asks to count how many separate groups of connected '1's (islands) are present.
Final Answer:
Count how many groups of connected '1's (islands) exist -> Option C
Quick Check:
Number of islands = count of connected '1' groups [OK]
Hint: Islands = groups of connected '1's, count them [OK]
Common Mistakes:
Confusing island count with island size
Counting '0's instead of '1's
Calculating perimeter instead of count
2. Which of the following Python code snippets correctly starts a DFS from cell (i, j) in a grid to mark connected land?
easy
A. def dfs(i, j): if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] == '0': return; grid[i][j] = '0'; dfs(i+1,j); dfs(i-1,j); dfs(i,j+1); dfs(i,j-1)
B. def dfs(i, j): while grid[i][j] == '1': grid[i][j] = '0'; dfs(i+1,j); dfs(i-1,j)
C. def dfs(i, j): if grid[i][j] == '1': return; dfs(i+1,j); dfs(i-1,j)
D. def dfs(i, j): for x in range(len(grid)): for y in range(len(grid[0])): dfs(x,y)
Solution
Step 1: Check boundary and water conditions
def dfs(i, j): if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] == '0': return; grid[i][j] = '0'; dfs(i+1,j); dfs(i-1,j); dfs(i,j+1); dfs(i,j-1) correctly checks if indices are out of bounds or cell is water ('0') and returns.
Step 2: Mark current land as visited and recurse
def dfs(i, j): if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] == '0': return; grid[i][j] = '0'; dfs(i+1,j); dfs(i-1,j); dfs(i,j+1); dfs(i,j-1) marks grid[i][j] = '0' to avoid revisiting and calls dfs on all four neighbors.
Final Answer:
Option A code correctly implements DFS with boundary checks and marking visited -> Option A
Quick Check:
DFS with boundary and visited marking = def dfs(i, j): if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] == '0': return; grid[i][j] = '0'; dfs(i+1,j); dfs(i-1,j); dfs(i,j+1); dfs(i,j-1) [OK]
Hint: Check boundaries and mark visited before recursive DFS calls [OK]
Common Mistakes:
Not checking boundaries before recursion
Not marking visited cells
Using loops incorrectly inside DFS
3. What is the output of the following code snippet for the given grid?
grid = [
['1','1','0','0'],
['1','0','0','1'],
['0','0','1','1'],
['0','0','0','0']
]
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1':
count += 1
stack = [(i,j)]
while stack:
x,y = stack.pop()
if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == '1':
grid[x][y] = '0'
stack.extend([(x+1,y),(x-1,y),(x,y+1),(x,y-1)])
print(count)
medium
A. 2
B. 3
C. 4
D. 5
Solution
Step 1: Identify islands in the grid
The grid has three islands: top-left (cells (0,0),(0,1),(1,0)), single cell island at (1,3), and bottom-right island (cells (2,2),(2,3)).
Step 2: Trace the DFS using stack
Each '1' found triggers DFS marking connected '1's as '0'. The count increments for each new island found. Total islands counted = 3.
Final Answer:
3 -> Option B
Quick Check:
Number of islands found by DFS = 3 [OK]
Hint: Count connected '1' groups using DFS stack traversal [OK]
Common Mistakes:
Counting each '1' without marking visited
Misidentifying connected cells
Off-by-one errors in loops
4. The following DFS code for counting islands has a bug. What is the error?
def dfs(i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]):
return
if grid[i][j] == '0':
return
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1':
count += 1
dfs(i, j)
print(count)
medium
A. The DFS only checks horizontal neighbors
B. The boundary checks are incorrect
C. The count variable is not incremented properly
D. The code does not mark visited cells, causing infinite recursion
Solution
Step 1: Analyze DFS function behavior
The DFS checks boundaries and water but never marks visited land cells as '0' or visited.
Step 2: Identify consequence of missing visited marking
Without marking visited, DFS revisits same cells repeatedly, causing infinite recursion or stack overflow.
Final Answer:
The code does not mark visited cells, causing infinite recursion -> Option D