The As Far from Land as Possible interview question involves a 2D grid of 0s (water) and 1s (land). Your goal is to find a water cell such that its distance to the nearest land cell is maximized. The distance used is the Manhattan distance: dist(x, y) = |x1 - x2| + |y1 - y2|. This As Far from Land as Possible coding problem is a classic distance-mapping challenge in a matrix.
Companies like Microsoft and Bloomberg use this to test multi-source Breadth-First Search (BFS). It evaluates your ability to find the shortest path from multiple sources simultaneously, which is a highly efficient way to solve "nearest neighbor" problems in a grid. It also tests edge-case handling (e.g., grids with only water or only land).
This problem is best solved using the Array, Matrix, Breadth-First Search, Dynamic Programming interview pattern. You start a multi-source BFS from all land cells (1s) simultaneously. As the BFS expands into water cells (0s), you track the "level" or distance. The maximum level reached by the BFS is the answer.
Grid:
1 0 1
0 0 0
1 0 1
For "nearest distance" problems in a grid with multiple sources, always consider multi-source BFS. It’s significantly more efficient than running a search for each individual target.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Moves to Spread Stones Over Grid | Medium | Solve | |
| 01 Matrix | Medium | Solve | |
| Disconnect Path in a Binary Matrix by at Most One Flip | Medium | Solve | |
| Maximum Number of Points with Cost | Medium | Solve | |
| Minimum Falling Path Sum | Medium | Solve |