Minimize Total Grid Distance to Every Building
Company: Waymo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Find an empty grid cell minimizing total shortest-path distance to all buildings while respecting obstacles and building traversal restrictions.
Read the full Waymo Software Engineer interview experience this question came from
Constraints
- grid has 1 through 50 rows and 1 through 50 columns.
- Cells are 0 for empty land, 1 for a building, and 2 for an obstacle; at least one building is present.
- Moves are orthogonal with unit cost. Only empty cells may be intermediate path cells; a building may be entered only as the destination.
- Choose an empty cell reaching every building and return its minimum total distance; otherwise return -1.
Examples
Input: ([[1, 0, 2], [0, 0, 0], [2, 0, 1]],)
Expected Output: 4
Explanation: Published sample 1: the center has distance two to each building.
Input: ([[1, 0, 1, 0, 1]],)
Expected Output: -1
Explanation: Published sample 2: the middle building cannot be used as an intermediate cell.