You are given a 2D grid representing a city map.
Each cell contains one of the following characters:
'O'— an open road (you can travel up, down, left, or right)'X'— a blocked road (cannot be passed through)'D'— a DashMart location
You are allowed to move in four directions:
up, down, left, right
You are given a list of query locations in:[row, col] format
For each query location, return the minimum distance to the nearest DashMart.
If the location is out of bounds or cannot reach any DashMart, return -1.
Example
City Map
0 1 2 3 4 5 6 7 8
0 ['X','O','O','D','O','O','X','O','X']
1 ['X','O','X','X','O','O','O','O','X']
2 ['O','O','O','D','X','X','O','X','O']
3 ['O','O','O','D','O','X','O','O','O']
4 ['O','O','O','O','O','X','O','O','X']
5 ['O','O','O','O','X','O','O','X','X']
Locations Input
[
[200, 200],
[1, 4],
[0, 3],
[5, 8],
[1, 8],
[5, 5]
]
Expected Output
[-1, 2, 0, -1, 6, 9]
Explanation Summary:
- (200,200) is out of grid →
-1 - (1,4) shortest path to nearest D → distance = 2
- (0,3) is directly on a D → 0
- (5,8) cannot reach any D →
-1 - (1,8) can reach → distance = 6
- (5,5) reaches → distance = 9
Function Signature
Complete the getClosestDashmartDistance function.
Parameters:
1. 2D_CHARACTER_ARRAY cityMap
2. 2D_INTEGER_ARRAY locations
Return:
INTEGER_ARRAY result
这道 DoorDash VO 的题非常经典:
在城市地图里,找每个位置到最近 DashMart 的最短距离。
重点考察你有没有 BFS 的系统思维。
✅ 为什么是 BFS?
因为地图是等权图,每走一步都是 +1。
所以 BFS 能保证“第一次走到 DashMart 就是最短距离”。
✅ 面试官真正想听的是:
1. 是否能想到用多源 BFS(Multi-source BFS)?
很多候选人会犯错:
❌ 对每个 location 都跑一次 BFS → TLE
✅ 正确做法:
“把所有 DashMart 当作 BFS 的起点,一起扩散出去。”
这样能一次性计算整个地图中 每一个点到最近 DashMart 的距离 。
查询时直接 O(1) 取结果。
2. 能不能正确处理边界条件
面试官会检测你是否考虑以下情况:
- 坐标越界 → -1
- 本来就是
'D'→ 0 - 被
'X'围住完全走不出去 → -1 - 多个 DashMart 彼此影响 → 一起算
这些细节决定你是否是“工程级”候选人。
3. 代码部分是否结构清晰
VO 不只是写 BFS,而是看你能不能把:
- 队列初始化
- 走四个方向
- 防止重复访问
- 记录距离数组
- 优雅处理查询
全部讲得有条有理。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Doordash、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Tiktok 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。