PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of shortest-path computation and distance labeling in grid-based graphs, measuring competency in algorithmic problem solving and efficient traversal strategies.

  • medium
  • DoorDash
  • Coding & Algorithms
  • Software Engineer

Find Each Cell's Nearest Source

Company: DoorDash

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given an `m x n` grid containing `1` for source cells and `0` for regular cells, compute for every cell the shortest 4-directional distance to any source cell. Moving up, down, left, or right costs 1 step. Return an `m x n` matrix of distances, where every source cell has distance `0`. If the grid contains no source cells, return `-1` for every cell.

Quick Answer: This question evaluates understanding of shortest-path computation and distance labeling in grid-based graphs, measuring competency in algorithmic problem solving and efficient traversal strategies.

Given an m x n grid where 1 represents a source cell and 0 represents a regular cell, compute the shortest distance from every cell to any source cell. You may move up, down, left, or right, and each move costs 1 step. Return an m x n matrix of distances. Every source cell has distance 0. If the grid contains no source cells, return -1 for every cell. If the grid is empty, return an empty list.

Constraints

  • 0 <= m
  • 0 <= n
  • 0 <= m * n <= 100000
  • grid[i][j] is either 0 or 1
  • All rows in grid have the same length

Examples

Input: ([[0,0,0],[0,1,0],[0,0,0]],)

Expected Output: [[2,1,2],[1,0,1],[2,1,2]]

Explanation: The source is in the center. Each cell's distance is its Manhattan distance to the center source.

Input: ([[1,0,0],[0,0,0],[0,0,1]],)

Expected Output: [[0,1,2],[1,2,1],[2,1,0]]

Explanation: There are two sources. Each cell uses the shorter distance to either the top-left or bottom-right source.

Input: ([[0,0],[0,0]],)

Expected Output: [[-1,-1],[-1,-1]]

Explanation: There are no source cells, so every output cell is -1.

Input: ([[1]],)

Expected Output: [[0]]

Explanation: The only cell is a source, so its distance is 0.

Input: ([],)

Expected Output: []

Explanation: The grid is empty, so the result is also an empty list.

Hints

  1. Instead of running a search from every regular cell, consider starting from all source cells at the same time.
  2. A breadth-first search processes cells in increasing distance order when every move has the same cost.
Last updated: Jun 17, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Validate a Shopping Cart - DoorDash (medium)
  • Calculate Driver Payments - DoorDash (medium)
  • Implement Timeout Refund Workflow - DoorDash (medium)
  • Maximize Chef Assignment Profit - DoorDash (medium)
  • Compute Courier Delivery Pay - DoorDash (easy)