← DoorDash Interview InsightsSoftware Engineer
Spotted multi-source BFS pretty fast, that part wasn't the issue.
Use multi-source BFS starting from all source cells simultaneously, updating distances level by level. Initialize a distance matrix with -1 (or infinity) and set source cells to 0, then enqueue them. If no sources exist, return a matrix filled with -1.
Pro tip: Emphasize that multi-source BFS guarantees the shortest path in unweighted grids and runs in O(m*n) time, which is optimal. Also mention handling edge cases like empty grid or no sources early to avoid unnecessary work.
Confirm grid dimensions, 4-directional movement, and behavior when no sources exist. Discuss handling empty grid or all zeros.
Create a distance matrix filled with -1 (or a large number) and a queue. Iterate through the grid to set source cells to 0 and enqueue their coordinates.
While the queue is not empty, pop a cell and explore its four neighbors. For each unvisited neighbor, set its distance to current distance + 1 and enqueue it.
If the queue is empty after initialization (no sources), return a matrix filled with -1 immediately.
Return the distance matrix. State time complexity O(m*n) and space complexity O(m*n) for the queue and distance matrix.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.