FastPrepFastPrep
Problem Brief

Closest DashMart

PHONE SCREEN

A DashMart is a warehouse run by DoorDash that houses items found in convenience stores, grocery stores, and restaurants. We have a city with open roads, blocked-off roads, and DashMarts.

City planners want you to identify how far a location is from its closest DashMart.

You can only travel over open roads (up, down, left, right).

Locations are given in [row, col] format.

Provided:

  • city: char[][] - A 2D array representing the city where ' ' represents an open road, 'X' represents a blocked road, and 'D' represents a DashMart.
  • locations: int[][2] - A list of pairs [row, col] representing the locations to check.

Return:

answer: int[] - Return a list of the distances from a given point to its closest DashMart. If a DashMart cannot be reached from a location, return -1 for that location.

1Example 1

Input
city = [ ['X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'], ['X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'], [' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '], [' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'], [' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X'] ], locations = [[200, 200], [1, 4], [0, 3], [5, 8], [1, 8], [5, 5]]
Output
[-1, 2, 0, -1, 6, 9]
Explanation
đŸ„
public int[] closestDashMart(char[][] city, int[][] locations) {
  // write your code here
}
Input

city

[ ['X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'], ['X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'], [' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '], [' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'], [' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X'] ]

locations

[[200, 200], [1, 4], [0, 3], [5, 8], [1, 8], [5, 5]]

Output

[-1, 2, 0, -1, 6, 9]

Sign in to submit your solution.