← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE coding round, pretty standard grid traversal problem. Nothing too wild but I definitely overthought the approach at first.

Questions Asked (1)

Q1

Given a 2D grid of '1's (land) and '0's (water), count the number of distinct islands. Islands are formed by horizontally or vertically connected land cells, and the grid borders are treated as water.

Algorithms & Data Structures
Author's notes

Spent like two minutes second-guessing whether to go BFS or DFS and probably looked indecisive.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Treat the grid as a graph and use DFS or BFS to traverse each unvisited land cell, marking all connected land cells as visited to count one island. Iterate through every cell, and when an unvisited '1' is found, increment the island count and explore its entire connected component.

Pro tip: Mention that you can modify the grid in-place to mark visited cells (e.g., changing '1' to '0') to save space, but clarify that this mutates the input; if mutation is not allowed, use a separate visited set or matrix. Also, discuss handling edge cases like empty grid or all water.

1. Clarify the problem and constraints

Confirm that islands are defined by 4-directional connectivity and that grid borders are water. Ask about grid size limits, whether the input can be modified, and if recursion depth is a concern.

2. Choose traversal method

Decide between DFS (recursive or iterative) and BFS. DFS is simpler but may risk stack overflow for large grids; BFS uses a queue and avoids recursion limits.

3. Implement traversal to mark visited

For each unvisited land cell, start a traversal that marks all connected land cells as visited. Use a visited set/matrix or modify the grid in-place (e.g., set to '0').

4. Count islands and handle boundaries

Increment the island count each time you start a traversal from an unvisited land cell. Ensure boundary checks prevent out-of-bounds access.

5. Analyze complexity and test

State time complexity O(M*N) and space complexity O(M*N) for visited set or O(min(M,N)) for BFS queue in worst case. Walk through a small example and edge cases.

Key Points to Mention

  • Graph traversal (DFS/BFS) on a 2D grid
  • Visited tracking: in-place modification vs. separate data structure
  • Time complexity O(M*N) and space complexity trade-offs
  • Handling edge cases: empty grid, all water, all land, single row/column
  • Recursion depth concerns and iterative alternatives
  • Connectivity definition: 4-directional (up, down, left, right)

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