Compute nearest dashmart distances for queries
DoorDashSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's understanding of graph traversal and shortest-path computation on grids, spatial reasoning about obstacles and sources, and the ability to analyze algorithmic scalability and complexity when answering many queries.
You are given an m x n grid where each cell is one of: -1 for a wall (impassable), 0 for a dashmart (a store), and INF = 2^31 - 1 for an empty room. Movement is allowed 4-directionally with cost 1 per step. You are also given an array queries of k coordinates (r, c), each guaranteed to be an empty room. For each query, return the length of the shortest path to any dashmart; return -1 if no dashmart is reachable or if the grid contains no dashmarts. Implement a function nearestDistances(grid, queries) that returns an array of k integers in the order of queries. Discuss and implement an approach that scales when k is large, and analyze its time and space complexity. Include how you would handle edge cases such as: duplicate queries, rooms completely blocked by walls, empty list of queries, or grids with zero dashmarts.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.