Trapping Rain Water

Algorithms
Hard
Meta
101.5K views

Given $n$ non-negative integers representing an elevation map, compute how much water it can trap after raining. Solve using a Two-Pointer approach.

Why Interviewers Ask This

Meta asks this problem to evaluate a candidate's ability to optimize space complexity while maintaining linear time performance. It tests logical reasoning, pattern recognition in array traversal, and the capacity to move beyond naive brute-force solutions. The interviewer specifically looks for how you handle edge cases and whether you can derive the two-pointer optimization independently without relying on pre-memorized patterns.

How to Answer This Question

1. Clarify the problem by confirming input constraints and defining what constitutes trapped water based on left and right boundaries. 2. Propose the brute-force solution first to establish a baseline, explaining its O(n^2) time and O(1) space limitations. 3. Introduce the optimized Two-Pointer approach: initialize pointers at both ends and track max heights seen so far from each side. 4. Explain the core logic: if the left height is smaller, calculate water using the left max; otherwise, use the right max, moving the pointer with the smaller height inward. 5. Walk through a concrete example like [0,1,0,2,1,0,1,3,2,1,2,1] step-by-step to demonstrate the pointer movement and water accumulation calculation. 6. Conclude by analyzing the final complexity: O(n) time and O(1) space, emphasizing why this meets Meta's efficiency standards.

Key Points to Cover

  • Demonstrating the derivation of the two-pointer logic rather than just reciting code
  • Explicitly stating the O(n) time and O(1) space complexity advantages
  • Handling edge cases like empty arrays or single-element inputs gracefully
  • Using a concrete numerical example to validate the logic during the explanation
  • Showing awareness of Meta's focus on scalable and memory-efficient algorithms

Sample Answer

To solve the Trapping Rain Water problem efficiently, I would first ensure we understand that water at any index depends on the minimum of the maximum heights to its left and right, minus the current height. A naive approach would check every element against all others, resulting in O(n^2) time complexity, which is inefficient for large datasets often seen at Meta. Instead, I propose a Two-Pointer approach. We start with 'left' at index 0 and 'right' at index n-1. We maintain two variables, 'leftMax' and 'rightMax', initialized to zero. While left is less than right, we compare heights[left] and heights[right]. If heights[left] is smaller, we know the water level is determined by the left side because the right boundary is guaranteed to be higher or equal. We update leftMax, calculate trapped water as leftMax - heights[left], add it to our total, and increment left. Conversely, if heights[right] is smaller, we perform the same logic for the right side. This ensures we only traverse the array once. For example, with input [0,1,0,2,1,0,1,3,2,1,2,1], the pointers converge while tracking maxes. At index 2 (height 0), the leftMax is 1, so we trap 1 unit. This continues until pointers meet. This method achieves O(n) time and O(1) space, demonstrating optimal resource utilization and strong algorithmic thinking.

Common Mistakes to Avoid

  • Failing to explain the intuition behind why moving the smaller height pointer is valid
  • Neglecting to mention the space complexity benefit compared to the dynamic programming approach
  • Getting lost in code implementation details before clarifying the algorithmic strategy
  • Overlooking the case where the array is too short to trap any water

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 71 Meta questions