As reported: Amazon SDE-1 loop (Bangalore, April 2025): two DSA questions in 45 minutes per round plus 15 minutes of introduction and Leadership Principles; statements were vague on purpose and clarifying questions were expected. Shortest Bridge was one of the questions.
This is LeetCode 934 — Shortest Bridge.
You are given an n × n binary grid where 1 is land and 0 is water. An island is a group of 1s connected 4-directionally, and the grid contains exactly two islands. You may change 0s into 1s to connect the two islands into one. Return the smallest number of 0s you must flip.
Example 1
grid = [[0,1],
[1,0]]
Output: 1
Example 2
grid = [[0,1,0],
[0,0,0],
[0,0,1]]
Output: 2
Constraints: 2 ≤ n ≤ 100; grid[i][j] is 0 or 1; there are exactly two islands.
Code runner format: LeetCode's test-case format — grid on one line. Implement shortestBridge(grid); the harness prints the number.