Magicsheet logo

Trapping Rain Water II

Hard
48.5%
Updated 6/1/2025

Trapping Rain Water II

What is this problem about?

The "Trapping Rain Water II coding problem" is a 3D extension of the classic water-trapping challenge. Instead of a 1D elevation map, you are given a 2D matrix representing the heights of a terrain. You need to calculate the total volume of water that can be trapped within this terrain after a heavy rain. This problem is significantly more complex because water can leak in four directions (North, South, East, West), and the "spill point" depends on the lowest boundary around any given cell.

Why is this asked in interviews?

This "Trapping Rain Water II interview question" is a favorite for high-level technical interviews at companies like Google and Bloomberg. it tests your ability to adapt a well-known 1D algorithm to a more complex 2D space. It specifically evaluates your mastery of priority queues (heaps) and graph traversal algorithms like Breadth-First Search (BFS). Understanding how to "shrink" a boundary inward to find the water level is a sophisticated problem-solving technique.

Algorithmic pattern used

The "Array, Matrix, Breadth-First Search, Heap (Priority Queue) interview pattern" is the standard way to solve this. We start by adding all cells on the outer boundary of the matrix into a min-heap. We then repeatedly extract the cell with the lowest height from the heap and explore its neighbors. For each neighbor, if its height is less than the current boundary height, it traps water equal to the difference. The neighbor is then added to the heap with a height equal to max(neighbor_height, boundary_height).

Example explanation

Imagine a 3x3 pool with a height of 1 in the center and 5 on all edges.

  1. All edges (height 5) are added to the min-heap.
  2. The lowest edge cell (height 5) is popped.
  3. Its neighbor is the center cell (height 1).
  4. Since 1 < 5, water trapped at center = 5 - 1 = 4 units.
  5. If the center was surrounded by different heights (e.g., 5, 4, 6, 7), the water level would be determined by the lowest neighbor (4). This "inward-moving boundary" ensures we always find the lowest possible leakage point for any interior cell.

Common mistakes candidates make

A common mistake in the "Trapping Rain Water II coding problem" is attempting to use the 1D approach of "left-max" and "right-max" independently for rows and columns. This doesn't work because water can leak diagonally or through complex paths. Another error is not using a priority queue, which is essential to always process the "weakest" part of the boundary first.

Interview preparation tip

To master the "Array, Matrix, Breadth-First Search, Heap (Priority Queue) interview pattern," practice problems involving "Dijkstra-like" traversal on grids. Focus on understanding how a priority queue can be used to maintain a frontier that moves from the outside in. This technique is also useful for "shortest path in a weighted grid" problems.

Similar Questions