As reported: Coupang Bengaluru (Rocket Growth team), 60-minute phone screen (2025): in a grid of water and land, find the distance between the two closest islands — the minimum number of water cells between them. Also asked in a Coupang coding round (2026) with exactly two islands: the minimum number of water cells to turn into land to join them.
You are given an m × n grid where 1 is land and 0 is water. An island is a maximal group of land cells connected 4-directionally (up, down, left, right). The grid has at least two islands.
The distance between two islands is the minimum number of water cells on a 4-directional path from one to the other — equivalently, the fewest water cells you would have to turn into land to connect them. Return the distance between the two closest islands.
With exactly two islands this is LeetCode 934 — Shortest Bridge.
Example 1
grid = [[1,1,0,1,1],
[1,1,0,0,0],
[0,0,0,0,0],
[0,0,1,1,1]] -> 1
Three islands; the top-left and top-right ones are one water cell apart.
Example 2
grid = [[1,0,0,0,1],
[1,1,0,0,0],
[0,0,0,0,0],
[0,0,1,1,1]] -> 2
Three islands; several pairs are two water cells apart.
Example 3
grid = [[0,1,0],
[0,0,0],
[0,0,1]] -> 2
Constraints: 1 ≤ m, n ≤ 300; grid[i][j] is 0 or 1; there are at least two islands.
Code runner format: one line holding grid. Implement closestIslands(grid); the harness prints the distance.