Skip to content
Home » Top Amazon SDE 2 Interview Question: Water Flow Simulation in a Grid

Top Amazon SDE 2 Interview Question: Water Flow Simulation in a Grid

  • by

One of the most frequent coding challenges in Amazon SDE 2 interviews involves grids. Imagine a wall built with solid and porous bricks. Water begins at the ground level, and your task is to track how far upward the water can move and the highest point it reaches.

It may seem simple at first, but solving it requires careful understanding of how water spreads inside the wall, how boundaries affect its movement, and how to avoid blocked paths.

You can think of the wall as a graph problem. Porous bricks act as nodes, and the connections between them are edges. Using BFS or DFS, you can simulate water flow starting from the bottom row while ensuring that it does not pass through solid bricks. This kind of problem is designed to test whether you can take a real-world concept, model it as an algorithm, and implement a clean solution under interview pressure.

Brick Wall Problem Statement

You are given an m × n grid representing a wall:

  • x represents a solid brick

  • . represents a porous brick

Water flows through porous bricks and spreads to adjacent porous ones in four directions up, down, left, and right. Solid bricks block the flow. Since the wall sits on the ground, water can enter only through porous bricks in the bottom row.

Ideal Approach

We need to figure out how high water can rise in each column. Starting from all porous bricks in the bottom row, we perform BFS or DFS to find all reachable porous bricks. The maximum depth is then calculated as the difference between the bottom row and the highest row reached by the water.

Python Solution Using BFS

from collections import deque

def solve(brickWall):
n = len(brickWall)
m = len(brickWall[0]) if n > 0 else 0

visited = [[False for _ in range(m)] for _ in range(n)]
q = deque()

# Initialize BFS from porous bricks in bottom row
for j in range(m):
if brickWall[n-1][j] == ‘.’:
q.append((n-1, j))
visited[n-1][j] = True

dirs = [(-1,0), (1,0), (0,-1), (0,1)]
min_row = n – 1

while q:
r, c = q.popleft()
if r < min_row:
min_row = r
for dr, dc in dirs:
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < m:
if brickWall[nr][nc] == ‘.’ and not visited[nr][nc]:
visited[nr][nc] = True
q.append((nr, nc))

return n – 1 – min_row

Walkthrough Example

Grid:

0 | . x .
1 | . . x
2 | . x .

As input:

[
“.x.”,
“..x”,
“.x.”
]

The BFS starts at the porous cells in the bottom row which are positions (2,0) and (2,2). From (2,0) it can move to (1,0) and then to (0,0). From (2,2) the flow is blocked immediately by a solid brick. The highest reachable point is row 0. That means the maximum depth is 2 - 0 = 2.

Complexity

Time complexity is O(n × m) since each brick is processed only once. Space complexity is O(n × m) because we store visited states and use a queue for BFS.

Why This Matters in Amazon Interviews

This type of coding problem tests more than implementation. It shows whether you can convert a real situation into a graph model, apply the correct traversal method, and explain your reasoning clearly while solving under pressure.

How CareerXcelerator Helps

CareerXcelerator is designed to prepare you for these challenges.

  1. Technical Practice
    You get step by step guidance for solving graph and grid problems like this one. An AI mentor explains the logic behind solutions, gives hints, and helps you polish your approach and code clarity.

  2. Mock Interviews
    You can practice in real interview style environments and then receive detailed feedback on how you approached the problem, how clean your code was, and how well you communicated. This lets you identify your weak spots and fix them before the actual interview.

By combining problem solving practice with realistic interview simulation, CareerXcelerator ensures that you not only understand problems like the brick wall but also gain the confidence to solve them during interviews.

You can join our WhatsApp group where we provide access to mock interview sessions, daily job opportunities, and personal mentor guidance. It is the fastest way to prepare smarter, stay updated, and move closer to your dream role at Amazon.

Leave a Reply

Your email address will not be published. Required fields are marked *