← Coupang Interview Insights

Coupang·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Got a coding problem from Coupang that's basically a graph traversal puzzle. Pretty classic BFS/DFS setup but the two-phase approach tripped me up a bit under pressure.

Questions Asked (1)

Q1

Given an n x n binary matrix where 1s represent land and 0s represent water, and exactly two islands exist, find the minimum number of 0s you need to flip to connect the two islands into one.

Algorithms & Data Structures
Author's notes

The core idea isn't that hard once you see it: BFS/DFS to mark one island, then BFS outward from it counting steps until you hit the second island.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS/DFS to identify the two islands and then run a multi-source BFS from one island to find the shortest path to the other, where each step through water costs 1. Alternatively, compute the minimum Manhattan distance between any cell of island A and any cell of island B, but note that this may not always be correct due to obstacles; the BFS approach is more robust.

Pro tip: Clarify that the problem is equivalent to finding the shortest path in a grid where land cells of the same island have cost 0 and water cells have cost 1, and mention that a 0-1 BFS or Dijkstra can be used if movement is allowed in 4 directions. Also, discuss edge cases like islands touching diagonally or the grid being all water except two cells.

1. Identify the two islands

Traverse the grid to find the first '1', then use BFS/DFS to mark all cells of that island. Continue scanning to find the second island and mark its cells.

2. Choose a search strategy

Decide between multi-source BFS from one island or computing pairwise distances. Multi-source BFS is efficient and handles obstacles correctly.

3. Perform multi-source BFS

Initialize a queue with all cells of the first island, with distance 0. Expand in 4 directions; when encountering a water cell, increment distance; when encountering a cell of the second island, return the distance.

4. Optimize and handle edge cases

Use a visited set to avoid reprocessing. Consider if diagonal moves are allowed (usually not). If the islands are already connected, return 0.

5. Analyze complexity

Time complexity is O(n^2) since each cell is visited at most once. Space complexity is O(n^2) for the queue and visited set.

Key Points to Mention

  • BFS/DFS for island identification
  • Multi-source BFS for shortest path
  • Distance metric: number of water cells flipped
  • 4-directional movement (unless specified otherwise)
  • Time and space complexity analysis
  • Edge cases: islands already connected, no water cells, etc.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.