On this page
Trapping Rain Water
Solving Trapping Rain Water using Two pointers.
Problem
Water trapped between bars.
Pattern
This problem demonstrates the Two pointers pattern.
Approach
Track left/right max heights.
Solution
// Solution for Trapping Rain Water
// Pattern: Two pointers
// O(n) time, O(1) space
Complexity
O(n) time, O(1) space
Best Practices
- Identify the pattern before coding — pattern recognition saves time
- Handle edge cases: empty input, single element, duplicates
- Use descriptive variable names even in timed interviews
- Test with the provided examples plus one custom case