FastPrepTrapping Rain Water
Problems/Trapping Rain Water

Trapping Rain Water

Kotak Mahindra Bank logoKotak Mahindra Bank● HardFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names Trapping Rain Water without restating its full contract. This practice version uses the conventional task with explicit bounds, examples, and a runnable interface.

You are given an integer array height of length n. n vertical bars stand on the x-axis. The i-th bar has width 1 and height height[i].

Compute how many units of water the bars can trap after rain.

Water sits above a bar only when both a strictly taller left boundary and a strictly taller right boundary exist. The water depth at index i is max(0, min(leftMax[i], rightMax[i]) - height[i]), where leftMax[i] is the tallest bar at an index < i and rightMax[i] is the tallest bar at an index > i.

Function

trap(height: int[]) → int

Examples

Example 1

height = [0,1,0,2,1,0,1,3,2,1,2,1]return = 6

The bars trap 1 + 1 + 2 + 1 + 1 = 6 units of water.

Example 2

height = [4,2,0,3,2,5]return = 9

The valley between the height-4 and height-5 bars traps 2 + 4 + 1 + 2 = 9 units.

Constraints

  • 1 <= height.length <= 2 * 10^4.
  • 0 <= height[i] <= 10^5.

More Kotak Mahindra Bank problems

  • Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
  • Best Time to Buy and Sell Stock IIONSITE INTERVIEW · Seen Nov 2026
  • Maximum Non-Adjacent Coins in Two RowsONSITE INTERVIEW · Seen Oct 2026
All Kotak Mahindra Bank interview questionsSee Kotak Mahindra Bank hiring insights
Saved
class Solution {
    public int trap(int[] height) {
      // Write your code here.
    }
}
Checking account…