Distance from every cell to the nearest zero in a binary matrix

Quick Overview

Given a binary matrix, return the distance from every cell to its nearest zero when each step moves to an edge-adjacent cell. Tests shortest-path reasoning on a grid with many starting points and keeping the work linear in the number of cells.

Distance from every cell to the nearest zero in a binary matrix

Company: DoorDash

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an `m x n` binary matrix `mat`, return a matrix of the same shape where each cell holds the distance from that cell to the nearest `0` in `mat`. The distance between two adjacent cells is `1`. ### Function Signature ```python def update_matrix(mat: list[list[int]]) -> list[list[int]]: ``` ### Rules - Two cells are adjacent when they share an edge (up, down, left or right). Diagonal moves do not count. - A cell that holds `0` has distance `0`. - Return the distances as an `m x n` matrix of integers. ### Constraints - `m == len(mat)` and `n == len(mat[i])` - `1 <= m, n <= 10^4` - `1 <= m * n <= 10^4` - `mat[i][j]` is either `0` or `1`. - There is at least one `0` in `mat`. ### Examples ```text Input: mat = [[0,0,0],[0,1,0],[0,0,0]] Output: [[0,0,0],[0,1,0],[0,0,0]] ``` The only `1` is at position (1, 1). All four of its neighbors hold `0`, so its distance is `1`. ```text Input: mat = [[0,0,0],[0,1,0],[1,1,1]] Output: [[0,0,0],[0,1,0],[1,2,1]] ``` Cells (2, 0) and (2, 2) sit directly below a `0`, so each gets `1`. Cell (2, 1) has only `1` cells as neighbors, so it needs at least 2 steps, for example `(2,1) -> (2,0) -> (1,0)`. ```text Input: mat = [[0,1,1],[1,1,1],[1,1,1]] Output: [[0,1,2],[1,2,3],[2,3,4]] ``` With a single `0` in the corner, each cell's distance is its row index plus its column index, so the opposite corner is 4 steps away.

Overview: Given a binary matrix, return the distance from every cell to its nearest zero when each step moves to an edge-adjacent cell. Tests shortest-path reasoning on a grid with many starting points and keeping the work linear in the number of cells.

DoorDash logo
DoorDash
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an m x n binary matrix mat, return a matrix of the same shape where each cell holds the distance from that cell to the nearest 0 in mat.

The distance between two adjacent cells is 1.

Function Signature

def update_matrix(mat: list[list[int]]) -> list[list[int]]:

Rules

  • Two cells are adjacent when they share an edge (up, down, left or right). Diagonal moves do not count.
  • A cell that holds 0 has distance 0 .
  • Return the distances as an m x n matrix of integers.

Constraints

  • m == len(mat) and n == len(mat[i])
  • 1 <= m, n <= 10^4
  • 1 <= m * n <= 10^4
  • mat[i][j] is either 0 or 1 .
  • There is at least one 0 in mat .

Examples

Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0],[0,0,0]]

The only 1 is at position (1, 1). All four of its neighbors hold 0, so its distance is 1.

Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0],[0,1,0],[1,2,1]]

Cells (2, 0) and (2, 2) sit directly below a 0, so each gets 1. Cell (2, 1) has only 1 cells as neighbors, so it needs at least 2 steps, for example (2,1) -> (2,0) -> (1,0).

Input: mat = [[0,1,1],[1,1,1],[1,1,1]]
Output: [[0,1,2],[1,2,3],[2,3,4]]

With a single 0 in the corner, each cell's distance is its row index plus its column index, so the opposite corner is 4 steps away.