← Amazon Interview Insights

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

Intermediate
Jul 2026

Summary

Amazon SWE coding round, just one algorithmic problem about water flow on a 2D terrain grid. Pretty straightforward BFS/DFS territory but the edge cases took me longer than I'd like to admit.

Questions Asked (1)

Q1

Given a 2D terrain map of heights and a starting point where water drops, find all the grid cells that get wet as water flows from higher to lower neighboring cells.

Algorithms & Data Structures
Author's notes

My first instinct was BFS and I think that was right, but I fumbled the neighbor comparison for a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the terrain as a graph where each cell is a node with directed edges to lower or equal-height neighbors, then perform a traversal (BFS/DFS) from the starting point to find all reachable cells. Clarify edge cases like equal heights, boundaries, and whether water can flow diagonally before coding.

Pro tip: Discuss both BFS and DFS, but recommend BFS for its iterative nature and ability to stop early if only a count is needed. Also, mention that if the start is a local minimum, only that cell gets wet, and confirm whether water can flow to equal-height neighbors.

1. Clarify problem constraints

Ask about grid size, height range, flow rules (diagonal? equal heights?), and whether the start cell is always wet. Confirm output format (list of cells or count).

2. Model as graph traversal

Treat each cell as a node with edges to neighboring cells of lower or equal height. Use BFS or DFS from the start to find all reachable cells.

3. Choose and justify algorithm

Select BFS for level-order exploration and easy cycle handling, or DFS for simplicity. Explain time and space complexity: O(R*C) time and space in the worst case.

4. Handle edge cases

Consider start at boundary, start as local minimum, equal-height plateaus, and disconnected regions. Ensure visited set prevents infinite loops.

5. Test and optimize

Walk through a small example, then discuss potential optimizations like early termination if only count is needed, or using union-find for offline queries.

Key Points to Mention

  • Graph representation: cells as nodes, directed edges to lower/equal neighbors
  • BFS vs DFS trade-offs: BFS for shortest path/level order, DFS for simplicity
  • Visited set to avoid cycles and redundant processing
  • Time and space complexity: O(R*C) for both
  • Edge cases: equal heights, boundaries, start as local minimum
  • Potential optimizations: early termination, union-find for multiple queries

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