What is the Trapping Rain Water problem and how to solve it?

DSA
Hard
Flipkart
121.6K views

This question asks for the algorithm to calculate trapped water between bars. It tests two-pointer or stack techniques.

Why Interviewers Ask This

It tests spatial reasoning and array manipulation skills. Interviewers ask this to see if you can derive optimal solutions for geometric problems. It is a favorite in competitive programming.

How to Answer This Question

Explain the two-pointer approach: track max height from left and right. Calculate water at current index as min(maxLeft, maxRight) - height. Move pointers inward.

Key Points to Cover

  • Two-pointer technique
  • Max height tracking
  • Water level calculation
  • Optimal complexity

Sample Answer

The Trapping Rain Water problem calculates water trapped between elevation bars. Using two pointers, we track the maximum height seen so far from both ends. At each step, the water level is determined by the lower of the…

Common Mistakes to Avoid

  • Using DP array unnecessarily
  • Incorrect pointer movement
  • Ignoring boundary conditions

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 107 DSA questionsBrowse all 131 Flipkart questions