Machine Learning Engineer • Mid-Level
Machine Learning Engineer • Mid-Level
High QualityPrep Other Coding Questions Asked by Google
Continue with targeted prep from Google's coding question bank.
Interview Summary
The task was to find an empty cell with the minimum total shortest-path distance to all buildings in a two-dimensional grid, while accounting for obstacles and unreachable locations.
The problem was a variation of Shortest Distance from All Buildings (LeetCode 317). Beyond finding shortest paths, the important challenge was efficiently identifying empty cells reachable from every building. My review covered a BFS-based approach and an optimization using in-place grid markers to eliminate invalid candidates earlier.
Interview Details
Coding: Shortest Distance from All Buildings
You are given a two-dimensional grid containing buildings, empty land, and obstacles. Each cell has one of three values:
0: Empty land where a new facility can potentially be built.1: An existing building.2: An obstacle that cannot be crossed.
The objective is to identify an empty cell that minimizes the sum of its shortest-path distances to every building in the grid. Movement is allowed in four directions:
- Up
- Down
- Left
- Right
Diagonal movement is not permitted. Paths may pass through empty cells but cannot pass through buildings or obstacles.
Core Requirements
Given the grid, determine the minimum possible total distance from a single empty cell to all buildings. A valid location must satisfy the following conditions:
- The selected cell must originally contain
0. - Every building must be reachable from that cell.
- Each distance must be the shortest valid path between the selected cell and the corresponding building.
- The sum of those distances must be minimized across all valid empty cells.
Return the minimum total
Unlock Full Details
Create a free account to unlock this interview experience report.