← Amazon Interview Insights

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

IntermediatePrefer not to say
Jun 2026Remote

Summary

Amazon SWE coding round, got a grid traversal problem. Pretty standard stuff if you've done any BFS/DFS prep, but the pressure of it being Amazon made me second-guess myself more than I should have.

Questions Asked (1)

Q1

Given a 2D grid of '1's (land) and '0's (water), count the number of islands, where an island is a group of horizontally or vertically connected land cells surrounded by water.

Algorithms & Data Structures
Author's notes

Classic problem and I knew it, but I still fumbled around for a minute deciding between BFS and DFS before just going with DFS because it felt cleaner to write recursively.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a graph traversal algorithm like DFS or BFS to explore each unvisited land cell and mark all connected land cells as visited, incrementing the island count for each traversal. Alternatively, use Union-Find to group connected land cells and count distinct sets. Clearly explain the chosen approach, its complexity, and handle edge cases.

Pro tip: At Amazon, emphasize scalability and efficiency: discuss how your solution handles large grids and why you chose a particular traversal order. Mention that you can optimize space by modifying the grid in-place if allowed, but clarify the trade-off.

1. Clarify the problem

Confirm the definition of an island, connectivity (4-directional), and input constraints. Ask about grid size, mutability, and edge cases like empty grid.

2. Choose an approach

Select between DFS, BFS, or Union-Find based on trade-offs. Explain why your choice is suitable for the given constraints.

3. Outline the algorithm

Describe the steps: iterate through each cell, when encountering unvisited land, increment count and traverse all connected land cells, marking them visited.

4. Analyze complexity

State time and space complexity. For DFS/BFS: O(M*N) time and O(M*N) space in worst case (e.g., all land). For Union-Find: O(M*N α(M*N)) time and O(M*N) space.

5. Handle edge cases and test

Discuss edge cases: empty grid, all water, all land, single row/column. Walk through a small example to verify correctness.

Key Points to Mention

  • Graph traversal techniques: DFS (recursive/iterative), BFS, and Union-Find
  • Time and space complexity analysis for each approach
  • In-place modification vs. using a visited matrix
  • Handling edge cases such as empty grid or no land
  • Potential stack overflow with recursive DFS and how to mitigate (iterative DFS or BFS)
  • Scalability considerations for large grids (e.g., memory usage, parallelization)

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