FastPrepTrapping Rain Water
Problems/Trapping Rain Water

Trapping Rain Water

InMobi logoInMobi● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an InMobi onsite interview. The report gives the elevation map goal, equal bar width, a worked example, and the intended prefix-maximum approach. It does not show the original function signature. Our version is about 99% the same as the reported question.

You are given an array heights, where heights[i] is the height of the ith bar in an elevation map. Every bar has width 1.

Return the total amount of rainwater that can be trapped between the bars.

Function

trapRainWater(heights: long[]) → long

Examples

Example 1

heights = [3,0,0,2,0,4]return = 10

The bars trap 3 + 3 + 1 + 3 = 10 units of water at indices 1, 2, 3, and 4.

Example 2

heights = [8,1,8,2,4]return = 9

The first basin traps 7 units above the bar of height 1, and the last basin traps 2 units above the bar of height 2.

Constraints

  • 0 <= heights.length <= 10^6
  • 0 <= heights[i] <= 10^9
  • The answer fits in a signed long.

More InMobi Onsite Interview questions

  • Russian Doll EnvelopesONSITE INTERVIEW · Seen Jul 2026
  • Minimum and Maximum with Optimal ComparisonsONSITE INTERVIEW · Seen Dec 2025
  • Minimum Edges to Connect All ComponentsONSITE INTERVIEW · Seen Dec 2025
  • Shortest Path Visiting All NodesONSITE INTERVIEW · Seen Sep 2025
  • Maximum Sum of Non-Adjacent Tree NodesONSITE INTERVIEW · Seen Jan 2022
  • Word SquaresONSITE INTERVIEW · Seen Jan 2022
  • Maximum Length Pair ChainONSITE INTERVIEW · Seen Jul 2021
  • Top View of a Binary TreeONSITE INTERVIEW · Seen Jul 2021
  • Maximum XOR Between Two ArraysONSITE INTERVIEW · Seen May 2021
  • Validate Binary Search TreeONSITE INTERVIEW · Seen May 2021

Similar questions at other companies

  • Count Hotspot Playback WindowsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Subarray with a Given SumExpedia · ONSITE INTERVIEWExpedia interview questions
  • Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
  • Running Through Walls with Limited EnergyScale AI · OAScale AI interview questions
  • Eating CandiesTikTok · OATikTok interview questions
  • Get Minimum Round Trip CostAmazon · OA
  • Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Run-Length Encoded Vector Dot ProductMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions
All InMobi interview questionsSee InMobi hiring insights
Saved
class Solution {
    public long trapRainWater(long[] heights) {
        // write your code here
    }
}
Checking account…