Compute the perimeter of a single island in a binary grid

Quick Overview

Given a grid of land and water cells that holds exactly one island with no lakes, return the length of the island's perimeter. The problem tests grid traversal, careful edge counting between neighboring cells, and safe handling of the grid boundary.

Compute the perimeter of a single island in a binary grid

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a 2D grid `grid` with `rows` rows and `cols` columns. Each cell is either land (`1`) or water (`0`). Cells are connected when they share an edge; diagonal neighbors are not connected. Everything outside the grid is water, and the land forms exactly one island, a single connected group of land cells. The island has no lakes: no pocket of water is enclosed by the island, so all water in the grid connects to the water around it. Each cell is a 1 by 1 square. Return the total perimeter of the island. ### Function Signature ```python def island_perimeter(grid: list[list[int]]) -> int: ``` ### Rules - `grid` is a list of rows. Every row is a list of `0` and `1` values, and all rows have the same length. - The perimeter is the number of cell sides that belong to a land cell and touch either a water cell or the outside of the grid. A side shared by two land cells is not part of the perimeter. ### Constraints - `1 <= rows, cols <= 125`, where `rows = len(grid)` and `cols = len(grid[0])` - `grid[i][j]` is `0` or `1` - There is exactly one connected region of land in the grid, and it has no lakes. ### Examples Example 1: ```text Input: grid = [[0, 0, 0, 1], [0, 1, 1, 1], [0, 0, 1, 0]] Output: 12 ``` Writing cells as (row, column), four edges sit between two land cells: (0,3) and (1,3), (1,1) and (1,2), (1,2) and (1,3), (1,2) and (2,2). The five land cells have 20 sides, and removing both copies of those four shared edges leaves 20 minus 8, which is 12. Example 2: ```text Input: grid = [[1]] Output: 4 ``` A single land cell has all four of its sides on the edge of the grid. Example 3: ```text Input: grid = [[1, 1], [1, 1]] Output: 8 ``` The island is a 2 by 2 square. Its four inner edges are shared between land cells, so only the eight outer sides count.

Overview: Given a grid of land and water cells that holds exactly one island with no lakes, return the length of the island's perimeter. The problem tests grid traversal, careful edge counting between neighboring cells, and safe handling of the grid boundary.

Bloomberg logo
Bloomberg
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a 2D grid grid with rows rows and cols columns. Each cell is either land (1) or water (0).

Cells are connected when they share an edge; diagonal neighbors are not connected. Everything outside the grid is water, and the land forms exactly one island, a single connected group of land cells.

The island has no lakes: no pocket of water is enclosed by the island, so all water in the grid connects to the water around it. Each cell is a 1 by 1 square. Return the total perimeter of the island.

Function Signature

def island_perimeter(grid: list[list[int]]) -> int:

Rules

  • grid is a list of rows. Every row is a list of 0 and 1 values, and all rows have the same length.
  • The perimeter is the number of cell sides that belong to a land cell and touch either a water cell or the outside of the grid. A side shared by two land cells is not part of the perimeter.

Constraints

  • 1 <= rows, cols <= 125 , where rows = len(grid) and cols = len(grid[0])
  • grid[i][j] is 0 or 1
  • There is exactly one connected region of land in the grid, and it has no lakes.

Examples

Example 1:

Input: grid = [[0, 0, 0, 1], [0, 1, 1, 1], [0, 0, 1, 0]]
Output: 12

Writing cells as (row, column), four edges sit between two land cells: (0,3) and (1,3), (1,1) and (1,2), (1,2) and (1,3), (1,2) and (2,2). The five land cells have 20 sides, and removing both copies of those four shared edges leaves 20 minus 8, which is 12.

Example 2:

Input: grid = [[1]]
Output: 4

A single land cell has all four of its sides on the edge of the grid.

Example 3:

Input: grid = [[1, 1], [1, 1]]
Output: 8

The island is a 2 by 2 square. Its four inner edges are shared between land cells, so only the eight outer sides count.