Max Area of Island
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement Solution.maxAreaOfIsland(grid). grid contains 0 water and 1 land. Return the maximum area of a four-directionally connected island, or zero when no land exists.
Starter code
class Solution:
def maxAreaOfIsland(self, grid):
passTest cases
two-components
{
"args": [
[
[
1,
1,
0,
0
],
[
1,
0,
0,
1
],
[
0,
0,
1,
1
]
]
]
}Expected: 3
Wizard outline
- Step 1: Initialize Solution.maxAreaOfIsland
Replace the empty starter with the first real state owned by Solution.maxAreaOfIsland. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the All Water case
Complete the readable core algorithm for one representative Interview case. Make flood fill return one plus the areas of all unvisited land neighbors.
- Step 4: Harden the Two Components boundary
Repair the reviewed boundary and pass the complete submission contract. DFS visits each cell in a component exactly once and sums one per cell, so it returns that island's exact area. The scan starts a traversal for every component and the maximum is therefore optimal.
Footguns and prerequisites
- Mark a land cell visited before recursing or cycles between adjacent cells cause repeated counting.
- trees and graphs
Reviewed references
Practice prerequisites
- Visit Each Grid Component(opens in a new tab)
Visit Each Grid Component isolates a cell is marked visited before being added to the frontier, so every active cell contributes to exactly one component. That focused state discipline is required when implementing max area of island as a complete Interview Problem.
Recommended approach and implementation
Scan cells and DFS from each land cell. Change visited land to water and return one plus four neighbor areas; track the maximum returned component size.
Why it works: DFS visits each cell in a component exactly once and sums one per cell, so it returns that island's exact area. The scan starts a traversal for every component and the maximum is therefore optimal.
class Solution:
def maxAreaOfIsland(self, grid):
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
rows, columns = len(grid), len(grid[0])
def area(row, column):
inside = 0 <= row < rows and 0 <= column < columns
if not inside or grid[row][column] == 0:
return 0
grid[row][column] = 0
return 1 + sum((
area(row + 1, column),
area(row - 1, column),
area(row, column + 1),
area(row, column - 1),
))
best = 0
for row in range(rows):
for column in range(columns):
best = max(best, area(row, column))
return best