Compute the Minimum Time for Any Robot to Reach Each Cell of a Grid with Obstacles

Quick Overview

Given a grid of empty cells, robots and obstacles, compute for every cell the fewest moves any robot needs to reach it, using -1 for obstacles and unreachable cells. Tests shortest-path search on a grid with several starting points and careful handling of blocked and unreachable cells.

Compute the Minimum Time for Any Robot to Reach Each Cell of a Grid with Obstacles

Company: DoorDash

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 2D grid of size `m x n` where each cell contains one of three values: - `0` is an empty cell. - `1` is a robot. - `2` is an obstacle. Calculate the minimum time for any robot to reach each empty cell. A robot can move in four directions (up, down, left, right), and each move takes one unit of time. Return a grid of the same dimensions where each cell contains the minimum time for any robot to reach it. Robot cells have a value of `0`. If an empty cell cannot be reached by any robot, its value should be `-1`. Obstacle cells should also be `-1`. ### Function Signature ```python def robot_arrival_times(grid: list[list[int]]) -> list[list[int]]: ``` ### Rules - A robot never enters an obstacle cell and never leaves the grid. - Robots move independently and do not block each other, so the value of an empty cell is the number of moves on the shortest obstacle-free path from the nearest robot. - If the grid has no robot, every cell of the result is `-1`. ### Constraints - `m == len(grid)` - `n == len(grid[i])` - `1 <= m, n <= 100` - `grid[i][j]` is `0`, `1` or `2` ### Examples ```text Input: grid = [[1, 0, 0], [0, 2, 0], [0, 0, 1]] Output: [[0, 1, 2], [1, -1, 1], [2, 1, 0]] ``` The robots are at `(0, 0)` and `(2, 2)` and the obstacle is at `(1, 1)`. Cells `(0, 1)` and `(1, 0)` are one step from `(0, 0)`, cells `(1, 2)` and `(2, 1)` are one step from `(2, 2)`, and the corners `(0, 2)` and `(2, 0)` are two steps from either robot. ```text Input: grid = [[0, 2, 0], [0, 1, 0], [0, 2, 0]] Output: [[2, -1, 2], [1, 0, 1], [2, -1, 2]] ``` The robot is at `(1, 1)`, and the obstacles at `(0, 1)` and `(2, 1)` block the vertical moves through the center. Each corner is reached in 2 steps through `(1, 0)` or `(1, 2)`. ```text Input: grid = [[1, 2, 0], [0, 2, 0]] Output: [[0, -1, -1], [1, -1, -1]] ``` The obstacles in column 1 cut the right column off from the robot at `(0, 0)`. Cell `(1, 0)` is one step away, and the empty cells `(0, 2)` and `(1, 2)` cannot be reached, so they are `-1`.

Overview: Given a grid of empty cells, robots and obstacles, compute for every cell the fewest moves any robot needs to reach it, using -1 for obstacles and unreachable cells. Tests shortest-path search on a grid with several starting points and careful handling of blocked and unreachable cells.

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

You are given a 2D grid of size m x n where each cell contains one of three values:

  • 0 is an empty cell.
  • 1 is a robot.
  • 2 is an obstacle.

Calculate the minimum time for any robot to reach each empty cell. A robot can move in four directions (up, down, left, right), and each move takes one unit of time.

Return a grid of the same dimensions where each cell contains the minimum time for any robot to reach it. Robot cells have a value of 0. If an empty cell cannot be reached by any robot, its value should be -1. Obstacle cells should also be -1.

Function Signature

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

Rules

  • A robot never enters an obstacle cell and never leaves the grid.
  • Robots move independently and do not block each other, so the value of an empty cell is the number of moves on the shortest obstacle-free path from the nearest robot.
  • If the grid has no robot, every cell of the result is -1 .

Constraints

  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 100
  • grid[i][j] is 0 , 1 or 2

Examples

Input: grid = [[1, 0, 0], [0, 2, 0], [0, 0, 1]]
Output: [[0, 1, 2], [1, -1, 1], [2, 1, 0]]

The robots are at (0, 0) and (2, 2) and the obstacle is at (1, 1). Cells (0, 1) and (1, 0) are one step from (0, 0), cells (1, 2) and (2, 1) are one step from (2, 2), and the corners (0, 2) and (2, 0) are two steps from either robot.

Input: grid = [[0, 2, 0], [0, 1, 0], [0, 2, 0]]
Output: [[2, -1, 2], [1, 0, 1], [2, -1, 2]]

The robot is at (1, 1), and the obstacles at (0, 1) and (2, 1) block the vertical moves through the center. Each corner is reached in 2 steps through (1, 0) or (1, 2).

Input: grid = [[1, 2, 0], [0, 2, 0]]
Output: [[0, -1, -1], [1, -1, -1]]

The obstacles in column 1 cut the right column off from the robot at (0, 0). Cell (1, 0) is one step away, and the empty cells (0, 2) and (1, 2) cannot be reached, so they are -1.