Distance From Every City Block to the Nearest Dasher Hub

Last updated: November 29, 2026

Quick Overview

Given a city grid where 'H' marks a Dasher hub, '#' a blocked block and '.' a street, return a matrix giving the fewest moves from each cell to its nearest hub, with -1 for blocked or unreachable cells.

DoorDash
Coding & Algorithms
Software Engineer
DoorDash
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a city grid where 'H' marks a Dasher hub, '#' a blocked block and '.' a street, return a matrix giving the fewest moves from each cell to its nearest hub, with -1 for blocked or unreachable cells.

A grid problem in the style of DoorDash onsites, tied to logistics questions such as where to place DashMart or pickup hubs. The interviewer wants to see multi-source BFS instead of running BFS from every cell, plus clean boundary handling.

What the Interviewer Expects
  • Recognize that starting BFS from all hubs at once gives nearest distances
  • Explain why running BFS per cell is O((rc)^2)
  • Mark distance on enqueue to avoid duplicates
  • Treat blocked cells and unreachable cells correctly
  • Discuss how weighted streets would change the algorithm
Key Topics to Cover
multi-source BFS
grids
shortest paths
matrix traversal
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you also return which hub is nearest to each cell?
  • What if some streets take two minutes to cross instead of one?
  • Where would you place one new hub to minimize the worst distance?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Grid of r x c characters: H hub, # blocked, . passable.
  • Moves are 4-directional with unit cost.
  • Output: dist[i][j], 0 for hubs, -1 for blocked or unreachable cells....
High-Level Design

Seed a single queue with every hub at distance 0. BFS explores in rings of increasing distance from the whole set of hubs, so the first time a cell is reached is via its nearest hub. Setting dist wh...


Submit Your Answer
Markdown supported