Limited Time Offer:Up to 0% off Hello Interview Premium
Up to 0% off Hello Interview Premium 🎉
Hello Interview
Learn Code
Introduction
Overview
Container With Most Water
Two Sum (Sorted Array)
3-Sum
Triangle Numbers
Move Zeroes
Sort Colors
Trapping Rain Water
Overview
Maximum Sum of Subarrays of Size K
Max Points You Can Obtain From Cards
Max Sum of Distinct Subarrays Length k
Overview
Longest Substring Without Repeating Characters
Longest Repeating Character Replacement
Overview
Can Attend Meetings
Insert Interval
Non-Overlapping Intervals
Merge Intervals
Employee Free Time
Overview
Valid Parentheses
Decode String
Longest Valid Parentheses
Monotonic Stack
Daily Temperatures
Largest Rectangle in Histogram
Overview
Linked List Cycle
Palindrome Linked List
Remove Nth Node From End of List
Reorder List
Swap Nodes in Pairs
Overview
Apple Harvest (Koko Eating Bananas)
Search in Rotated Sorted Array
Split Array Largest Sum
Kth Smallest Element in a Sorted Matrix
Minimum Shipping Capacity
Overview
Kth Largest Element in an Array
K Closest Points to Origin
Find K Closest Elements
Merge K Sorted Lists
Median from Data Stream
Introduction
Fundamentals
Return Values
Maximum Depth of Binary Tree
Path Sum
Passing Values Down and Helper Functions
Validate Binary Search Tree
Calculate Tilt
Diameter of a Binary Tree
Path Sum II
Longest Univalue Path
Graphs Overview
Adjacency List
Copy Graph
Graph Valid Tree
Matrices
Flood Fill
Number of Islands
Surrounded Regions
Pacific Atlantic Water Flow
Introduction
Overview
Level Order Sum
Rightmost Node
Zigzag Level Order
Maximum Width of Binary Tree
Graphs Overview
Minimum Knight Moves
Rotting Oranges
01-Matrix
Bus Routes
Overview
Word Search
Solution Space Trees
Subsets
Generate Parentheses
Combination Sum
Palindrome Partitioning
N-Queens
Overview
Course Schedule
Course Schedule II
Shortest Path Algorithms
Network Delay Time
Cheapest Flights Within K Stops
Path With Minimum Effort
Find City with Fewest Reachable
Fundamentals
Solving a Question with Dynamic Programming
Counting Bits
Decode Ways
Unique Paths
Maximal Square
Longest Increasing Subsequence
Word Break
Maximum Profit in Job Scheduling
Paint House
Paint House II
Minimum Window Subsequence
Overview
Best Time to Buy and Sell Stock
Gas Station
Jump Game
Jump Game II
Partition Labels
Overview
Implement Trie Methods
Prefix Matching
Overview
Count Vowels in Substrings
Subarray Sum Equals K
Spiral Matrix
Rotate Image
Set Matrix Zeroes
Vote For New Content
Pricing
Sign in / Sign up
Search
⌘K
Pricing

Tutor

Breadth-First Search

01-Matrix

medium

max (21)341224132;341225102;21365487109
Count: 10
abcValid triangle requires:a + b > c AND a + c > b AND b + c > a(every pair must sum to more than the third side)3511SOURCE23211SOURCE23UNREACHABLE$100$100$100$5000SRC123DST$100$100$1000SRC123DST01233141Threshold: 4Answer: 32 reachable01234231118Threshold: 2Answer: 01 reachable1102233321432263321
DESCRIPTION (inspired by Leetcode.com)

You are given an m x n binary matrix grid where each cell contains either a 0 or a 1.

Write a function that returns a matrix of the same dimensions where each cell contains the distance to the nearest 0 in the original matrix. The distance between two adjacent cells is 1. If there is no 0 in the grid, return -1 for each cell.

Example 1:

mat = [
  [1, 0, 1],
  [0, 1, 0],
  [1, 1, 1],
]

Output:

[
  [1, 0, 1],
  [0, 1, 0],
  [1, 2, 1],
]

Explanation

Since this question involves finding the shortest distance between each cell and the nearest 0, this is a good candidate for a breadth-first search (BFS) traversal.
The key idea here is to first enqueue all cells with value 0 into the queue. This allows us to gradually find the distance of each cell to the nearest 0 using a BFS traversal.
Let's look at example input grid to help visualize the process:
[
  [1, 0, 1],
  [0, 1, 0],
  [1, 1, 1],
]

Step 1: Initialize the Queue

We'll start by initializing the output grid with all cells set to -1 to indicate that the distance to the nearest 0 is unknown. We'll use BFS to gradually update the distance of each cell in the output grid.
Next, we can initialize the BFS queue with the positions of all cells that have a value of 0. We can also update the distances of these cells in the output grid to 0 directly (doing so also serves the purpose of marking these cells as visited).
# the coordinates of cells with value 0
queue = [ (0, 1), (1, 0), (1, 2) ]
# setting the distances of cells with value 0 to 0 in the output grid
output = [
    [-1, 0, -1],
    [0, -1, 0],
    [-1, -1, -1],
]

Step 2: Perform BFS Traversal (Distance 1)

Next, we'll start the BFS traversal with all the cells with value 0. All the neighbors of the cells currently in the BFS queue that currently have a value of -1 are at distance 1 from the nearest 0. We'll update the distances of these neighbors in the output grid to 1, and enqueue them into the BFS queue.
queue = [ (0, 0), (0, 2), (1, 1), (2, 0), (2, 2) ]
output = [
    [1, 0, 1],
    [0, 1, 0],
    [1, -1, 1],
]

Step 3: Perform BFS Traversal (Distance 2)

Now, all the neighbors of the cells currently in the BFS queue that have a value of -1 are at distance of 2 from the nearest 0. We'll update the distances of these neighbors in the output grid to 2, and enqueue them into the BFS queue.
queue = [ (2, 1) ]
output = [
    [1, 0, 1],
    [0, 1, 0],
    [1, 2, 1],
]

Step 4: Perform BFS Traversal (Distance 3)

At this step, all the neighbors of the cells in the BFS queue have been visited already, so we don't need to enqueue any more cells.
queue = []
output = [
    [1, 0, 1],
    [0, 1, 0],
    [1, 2, 1],
]

Step 5: Return the Output Grid

Since the queue is now empty, we can terminate the BFS traversal and return the output grid, which contains the distances of each cell to the nearest 0.
[
  [1, 0, 1],
  [0, 1, 0],
  [1, 2, 1],
]

Solution

Solution
Python
Language
from collections import deque
class Solution:
def updateMatrix(self, mat):
rows, cols = len(mat), len(mat[0])
output = [[-1] * cols for _ in range(rows)]
queue = deque()
# Step 1: Initialize the queue with all the 0 cells
# set their distance to 0 in the output grid
for r in range(rows):
for c in range(cols):
if mat[r][c] == 0:
queue.append((r, c))
output[r][c] = 0
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
# Step 2: Perform BFS traversal
distance = 1
while queue:
for _ in range(len(queue)):
r, c = queue.popleft()
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols:
if output[nr][nc] == -1:
output[nr][nc] = distance
queue.append((nr, nc))
distance += 1
# Step 5: Return the output grid
return output
Test Your Knowledge

Login to take the complexity quiz and track your progress

Complexity Analysis

Time Complexity: O(m * n) where m is the number of rows and n is the number of columns in the input matrix. We visit each cell at most once, and each cell is enqueued at most once.

Space Complexity: O(m * n) where m is the number of rows and n is the number of columns in the input matrix. The space required for the output matrix is O(m * n), and the space required for the queue can be at most O(m * n) in the worst case (e.g., when all cells are 0 and are enqueued at the start).

Mark as read

Next: Bus Routes

Your account is free and you can post anonymously if you choose.

Unlock Premium Coding Content

Interactive algorithm visualizations
Guided Practice
Recent interview questions
Learn More
Reading Progress

On This Page

Explanation

Solution

Questions
Meta SWE Interview QuestionsAmazon SWE Interview QuestionsGoogle SWE Interview QuestionsOpenAI SWE Interview QuestionsAnthropic SWE Interview QuestionsEngineering Manager (EM) Interview Questions
Learn
Learn System DesignLearn DSALearn BehavioralLearn ML System DesignLearn Low Level DesignGuided Practice
Links
FAQPricingGift PremiumHello Interview Premium
Legal
Terms and ConditionsPrivacy PolicySecurity
Contact
About UsProduct Support

7511 Greenwood Ave North Unit #4238 Seattle WA 98103


© 2026 Optick Labs Inc. All rights reserved.

Login to track your progress