Compute minimal flips connecting two islands
Company: Coupang
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates grid/graph traversal, connectivity and shortest-path reasoning, along with complexity analysis and handling of large inputs and input immutability.
Part 1: Minimum Water Flips to Connect Two Islands
Constraints
- 2 <= n <= 500
- grid[i][j] is either 0 or 1
- The grid contains exactly two disjoint 4-directionally connected islands
- The solution may mutate the input grid
Examples
Input: ([[0, 1], [1, 0]],)
Expected Output: 1
Explanation: The islands are diagonal. Flipping either remaining water cell connects them.
Input: ([[1, 0, 0], [0, 0, 0], [0, 0, 1]],)
Expected Output: 3
Explanation: The shortest 4-directional connection has three water cells between the two corner islands.
Input: ([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]],)
Expected Output: 1
Explanation: The center island is separated from the surrounding island by one layer of water.
Input: ([[1, 1, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0]],)
Expected Output: 3
Explanation: The closest cells of the two islands have Manhattan distance 4, so 3 water cells must be flipped.
Hints
- First mark all cells of one island, then treat them as simultaneous BFS starting points.
- The BFS layer number represents how many water cells have been flipped so far.
Part 2: Identify One Island, Then Expand Outward
Constraints
- 1 <= rows, cols <= 500
- grid[i][j] is either 0 or 1
- The grid contains exactly two disjoint 4-directionally connected islands
- The solution may mutate the input grid
Examples
Input: ([[0, 1], [1, 0]],)
Expected Output: [1, 1]
Explanation: The first island is the single cell at row 0, column 1. One flip connects it to the other island.
Input: ([[1, 1, 0, 1]],)
Expected Output: [2, 1]
Explanation: The first island has two cells. Flipping the middle water cell connects it to the right island.
Input: ([[0, 0, 0], [0, 1, 0], [0, 0, 1]],)
Expected Output: [1, 1]
Explanation: The first island is one cell, diagonally separated from the second island.
Input: ([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]],)
Expected Output: [16, 1]
Explanation: The first island is the outer ring with 16 cells. One flip connects it to the center island.
Hints
- Use an explicit stack or queue to collect every cell in the first island.
- After collecting the island, run a multi-source BFS from all its cells at once.
Part 3: Discover Islands Without Recursion
Constraints
- 0 <= rows <= 1000
- 0 <= cols <= 1000
- grid[i][j] is either 0 or 1
- Do not use recursive DFS
Examples
Input: ([],)
Expected Output: 0
Explanation: An empty grid has no islands.
Input: ([[1]],)
Expected Output: 1
Explanation: A single land cell forms an island of size 1.
Input: ([[0, 0], [0, 0]],)
Expected Output: 0
Explanation: There are no land cells.
Input: ([[1, 1, 0], [0, 1, 0], [1, 0, 1]],)
Expected Output: 3
Explanation: The largest island contains the connected cells at the top-left.
Input: ([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]],)
Expected Output: 10
Explanation: A long skinny island should be handled iteratively rather than recursively.
Hints
- Replace recursive calls with an explicit stack or queue of cells to visit.
- Mark cells as visited as soon as you push them, not when you pop them, to avoid duplicate pushes.
Part 4: Scalable Shortest Bridge for Very Large Grids
Constraints
- 1 <= rows, cols <= 2000
- rows * cols <= 4,000,000
- grid[i][j] is either 0 or 1
- The grid contains exactly two disjoint 4-directionally connected islands
- The solution may mutate the input grid
- Use an iterative approach; recursive DFS is not suitable at this scale
Examples
Input: ([[1, 0, 1]],)
Expected Output: 1
Explanation: A single water cell separates the two islands in a one-row grid.
Input: ([[1], [0], [0], [1]],)
Expected Output: 2
Explanation: Two water cells must be flipped in the single column.
Input: ([[1, 1, 0, 0, 0], [1, 1, 0, 1, 1]],)
Expected Output: 1
Explanation: The closest bridge is through the water cell at row 1, column 2.
Input: ([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]],)
Expected Output: 4
Explanation: The corner islands have Manhattan distance 5, so 4 water cells are between them.
Hints
- Encode a cell as one integer id = row * cols + col instead of storing many coordinate tuples.
- Mark cells directly in the grid to avoid keeping a separate visited matrix.
Part 5: Shortest Bridge Without Mutating the Input Grid
Constraints
- 1 <= rows, cols <= 1000
- grid[i][j] is either 0 or 1
- The grid contains exactly two disjoint 4-directionally connected islands
- Do not write to grid or any of its rows
Examples
Input: ([[0, 1], [1, 0]],)
Expected Output: 1
Explanation: The diagonal islands need one water flip, and the original grid should not be changed.
Input: ([[1, 1, 0, 0, 1]],)
Expected Output: 2
Explanation: Two water cells separate the left island from the right island.
Input: ([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]],)
Expected Output: 1
Explanation: The center island is one water layer away from the outer island.
Input: ([[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [1, 1, 0, 0, 0], [0, 1, 0, 0, 0]],)
Expected Output: 2
Explanation: The shortest bridge between the vertical upper island and lower-left island flips two water cells.
Hints
- Use a separate visited structure instead of marking cells inside grid.
- Once the first island is fully marked in visited, any unvisited land cell reached by BFS belongs to the second island.