← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

Amazon SWE coding round, one graph/BFS problem about finding the shortest bridge between two islands on a grid. Pretty standard algorithmic interview but the problem has a few layers that can trip you up if you're not careful.

Questions Asked (1)

Q1

Given a 2D grid of 0s (water) and 1s (land), find the minimum number of 0s you need to flip to connect two separate islands.

Algorithms & Data Structures
Author's notes

My first instinct was pure BFS from every water cell and I wasted probably five minutes going down that path before realizing I needed to find each island first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a shortest path on a graph where each cell is a node and edges connect adjacent cells with weight 0 for land and 1 for water. Use multi-source 0-1 BFS starting from all cells of one island to find the minimum cost to reach the other island, where cost equals the number of water cells flipped. Alternatively, label the islands and compute the minimum Manhattan distance between their cells minus 1.

Pro tip: Clarify assumptions upfront: whether flipping a 0 to 1 can connect diagonally or only orthogonally, and whether the two islands are guaranteed to exist. Mentioning edge cases and constraints (e.g., grid size) shows thoroughness and can guide the interviewer's expectations.

1. Clarify and Restate

Confirm the problem details: connectivity definition (4-directional vs 8-directional), input guarantees (exactly two islands), and output (minimum flips). Restate the problem in your own words to ensure alignment.

2. Identify Islands

Use BFS/DFS to find and label the two separate islands. Store the coordinates of cells belonging to each island for later use.

3. Model as Shortest Path

Treat each cell as a node; edges between adjacent cells have weight 0 if both are land, weight 1 if one is water (representing a flip). The goal is to find the minimum total weight path from any cell of island A to any cell of island B.

4. Run Multi-Source 0-1 BFS

Initialize a deque with all cells of island A at distance 0. Perform 0-1 BFS: when moving to a land cell, push front; to a water cell, push back and increment distance. Stop when reaching any cell of island B; the distance is the minimum flips.

5. Analyze Complexity and Optimize

State time and space complexity: O(R*C) time and space. Discuss potential optimizations like early termination or using Manhattan distance if only orthogonal moves are allowed and no obstacles exist.

Key Points to Mention

  • Graph modeling: cells as nodes, adjacency with weights 0/1.
  • Multi-source BFS from one island to efficiently compute minimum flips.
  • 0-1 BFS using deque for O(R*C) time, better than Dijkstra's O(R*C log(R*C)).
  • Handling edge cases: islands at borders, no water between them, large grids.
  • Alternative approach: compute minimum Manhattan distance between islands and subtract 1 (if only orthogonal moves and no obstacles).
  • Time and space complexity analysis: O(R*C) for both.

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