Two Pointers•Pattern: Two Pointers•Medium

Trapping rain water

Problem Intuition & Objective

Compute total water trapped between elevation bars.

Time ComplexityO(n)
Optimal
Space ComplexityO(1)
Memory Bounds
Optimal Algorithmic Approach

Two-pointer: always advance the smaller boundary side.

Core Invariants & Edge Cases

  • Water at i = min(maxLeft[i], maxRight[i]) - height[i]
  • Two-pointer O(1) space: advance the smaller-max side inward
  • leftMax and rightMax track running maximums from each side
Previous ProblemFour Sum — find all unique quadruplets summing to targetNext Problem Permutation in string (sliding window)

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.