Trapping Rain Water

Last updated: June 17, 2026

Quick Overview

Given an elevation map, compute how much water it can trap after raining. Can be solved with two pointers, stack, or DP. A classic Google interview favorite.

Google
Coding & Algorithms
Software Engineer
Google
June 17, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

84

0

111 solved


Given an elevation map, compute how much water it can trap after raining. Can be solved with two pointers, stack, or DP. A classic Google interview favorite.

How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem of trapping rainwater can be approached using the two-pointer technique. This method applies here because we need to calculate water trapped between bars of varying heights, which requires...

Approach
  1. Initialize two pointers, left at the beginning (index 0) and right at the end (last index) of the elevation array.
  2. Create two variables, left_max and right_max, to track the maximum heig...

Submit Your Answer
Markdown supported

Related Questions