Skip to content
Hello Python

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.numIslands(grid). grid contains '1' land and '0' water. Return the number of four-directionally connected land components.

Starter code

class Solution:
    def numIslands(self, grid):
        pass
Test cases

three-islands

{
  "args": [
    [
      [
        "1",
        "1",
        "0",
        "0"
      ],
      [
        "1",
        "0",
        "0",
        "1"
      ],
      [
        "0",
        "0",
        "1",
        "1"
      ]
    ]
  ]
}

Expected: 2

Wizard outline
  1. Step 1: Initialize Solution.numIslands

    Replace the empty starter with the first real state owned by Solution.numIslands. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. 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.

  3. Step 3: Pass the Three Islands case

    Complete the readable core algorithm for one representative Interview case. Start one flood fill per unvisited land cell and erase the complete component.

  4. Step 4: Harden the Diagonal Separate boundary

    Repair the reviewed boundary and pass the complete submission contract. The first scan encounter of each component increments once. Flood fill removes every other cell in that component before later scans, so no island is missed or counted twice.

Footguns and prerequisites
  • Diagonal land cells belong to different islands unless connected through horizontal or vertical steps.
  • 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 number of islands as a complete Interview Problem.

Recommended approach and implementation

Scan every cell. On land, increment the count and DFS four directions while changing that entire component to water.

Why it works: The first scan encounter of each component increments once. Flood fill removes every other cell in that component before later scans, so no island is missed or counted twice.

class Solution:
    def numIslands(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 erase(row, column):
            if not (0 <= row < rows and 0 <= column < columns) or grid[row][column] != '1': return
            grid[row][column] = '0'
            erase(row+1,column); erase(row-1,column); erase(row,column+1); erase(row,column-1)
        islands = 0
        for row in range(rows):
            for column in range(columns):
                if grid[row][column] == '1':
                    islands += 1
                    erase(row, column)
        return islands
Similar exercises