Compute each cell's distance to the nearest DashMart in a city grid with obstacles

Quick Overview

Given a city grid of stores, obstacles and empty cells, compute the shortest four-direction walking distance from every cell to its nearest store, using -1 for obstacles and cells that are cut off. It tests multi-source breadth-first search on a grid and the handling of unreachable regions.

Compute each cell's distance to the nearest DashMart in a city grid with obstacles

Company: DoorDash

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 2D grid that represents a city layout. Each cell holds one of three characters: - `'D'` is a DashMart location. - `'X'` is an obstacle and cannot be entered. - `'O'` is an empty cell. For every empty cell, find the shortest distance to the nearest DashMart. Distance is the number of steps taken moving up, down, left or right, and a path may pass only through cells that are not obstacles. Return a grid of the same shape that holds these distances. ### Function Signature ```python from typing import List def closest_dashmart(grid: List[List[str]]) -> List[List[int]]: ... ``` ### Rules - The output has the same number of rows and columns as `grid`. - A DashMart cell gets `0`. - An obstacle cell gets `-1`. - An empty cell that cannot reach any DashMart gets `-1`. If the grid has no DashMart at all, every cell is `-1`. - Each step goes to one of the four orthogonal neighbors. Diagonal moves and moves off the grid are not allowed. ### Constraints - `1 <= m, n <= 500`, where `m` is the number of rows and `n` is the number of columns - Every `grid[i][j]` is `'D'`, `'X'` or `'O'` ### Examples ```text Input: grid = [["O","O","D"],["O","X","O"],["D","O","O"]] Output: [[2,1,0],[1,-1,1],[0,1,2]] ``` Every empty cell next to a DashMart is one step away. The corners (0,0) and (2,2) are two steps from either DashMart, and the obstacle at (1,1) is `-1`. ```text Input: grid = [["D","X","O"],["X","X","O"],["O","O","O"]] Output: [[0,-1,-1],[-1,-1,-1],[-1,-1,-1]] ``` The only DashMart, at (0,0), is walled in by the obstacles at (0,1) and (1,0). No empty cell can reach it, so every empty cell is `-1`. ```text Input: grid = [["D","O","O"],["X","X","O"],["O","O","O"]] Output: [[0,1,2],[-1,-1,3],[6,5,4]] ``` The obstacles in row 1 force the path to the bottom-left corner around the right side: (0,0), (0,1), (0,2), (1,2), (2,2), (2,1), (2,0) is six steps.

Overview: Given a city grid of stores, obstacles and empty cells, compute the shortest four-direction walking distance from every cell to its nearest store, using -1 for obstacles and cells that are cut off. It tests multi-source breadth-first search on a grid and the handling of unreachable regions.

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

You are given a 2D grid that represents a city layout. Each cell holds one of three characters:

  • 'D' is a DashMart location.
  • 'X' is an obstacle and cannot be entered.
  • 'O' is an empty cell.

For every empty cell, find the shortest distance to the nearest DashMart. Distance is the number of steps taken moving up, down, left or right, and a path may pass only through cells that are not obstacles. Return a grid of the same shape that holds these distances.

Function Signature

from typing import List

def closest_dashmart(grid: List[List[str]]) -> List[List[int]]:
    ...

Rules

  • The output has the same number of rows and columns as grid .
  • A DashMart cell gets 0 .
  • An obstacle cell gets -1 .
  • An empty cell that cannot reach any DashMart gets -1 . If the grid has no DashMart at all, every cell is -1 .
  • Each step goes to one of the four orthogonal neighbors. Diagonal moves and moves off the grid are not allowed.

Constraints

  • 1 <= m, n <= 500 , where m is the number of rows and n is the number of columns
  • Every grid[i][j] is 'D' , 'X' or 'O'

Examples

Input: grid = [["O","O","D"],["O","X","O"],["D","O","O"]]
Output: [[2,1,0],[1,-1,1],[0,1,2]]

Every empty cell next to a DashMart is one step away. The corners (0,0) and (2,2) are two steps from either DashMart, and the obstacle at (1,1) is -1.

Input: grid = [["D","X","O"],["X","X","O"],["O","O","O"]]
Output: [[0,-1,-1],[-1,-1,-1],[-1,-1,-1]]

The only DashMart, at (0,0), is walled in by the obstacles at (0,1) and (1,0). No empty cell can reach it, so every empty cell is -1.

Input: grid = [["D","O","O"],["X","X","O"],["O","O","O"]]
Output: [[0,1,2],[-1,-1,3],[6,5,4]]

The obstacles in row 1 force the path to the bottom-left corner around the right side: (0,0), (0,1), (0,2), (1,2), (2,2), (2,1), (2,0) is six steps.