Prepin
Log in

Capturing Rainwater between a specified arrangement of bars.

Approach / Explanation

The problem "Trapping Rain Water between a given set of bars" is an algorithmic challenge frequently encountered in coding and IT interviews. It requires calculating the volume of water that can be trapped between a series of bars when it rains. These bars are represented as numbers in an array, with each number indicating the bar's height. The height of the trapped water pool is determined by the lowest height of the surrounding bars. A crucial insight for solving this problem is recognizing that a bar can only hold water if there are taller bars on both sides. To determine the amount of water that can be stored at each position, we need to assess the heights of the bars on either side.

Suggested Answer

One method to tackle this issue involves traversing the array and, for each element, determining the tallest bars on both the left and right sides. The volume of water that the current bar can hold is calculated as the minimum of the maximum heights on either side minus the height of the bar itself. In Python, the implementation might look like this: def findWater ( arr , n ) : # left[i] stores the height of the tallest bar to the left of the i'th bar left = [ 0 ] * n # right[i] stores the height of the tallest bar to the right of the i'th bar right = [ 0 ] * n # Initialize the result variable water = 0 # Populate the left array left [ 0 ] = arr [ 0 ] for i in range ( 1 , n ) : left [ i ] = max ( left [ i - 1 ], arr [ i ]) # Populate the right array right [ n - 1 ] = arr [ n - 1 ] for i in range ( n - 2 , - 1 , - 1 ) : right [ i ] = max ( right [ i + 1 ], arr [ i ]) ; # Compute the total water collected element by element for i in range ( 0 , n ) : water += min ( left [ i ], right [ i ] ) - arr [ i ] return water This method requires O(n) time and O(n) additional space, where n represents the number of elements in the input array.

Alternative Answer

Another method involves utilizing a two-pointer technique, which requires only O(1) additional space. def find_water ( arr , n ) : # Initializing the result result = 0 # Maximum element on both sides left_max = 0 right_max = 0 # Indices to traverse the array lo = 0 hi = n - 1 while ( lo <= hi ) : if ( arr [ lo ] < arr [ hi ] ) : if ( arr [ lo ] > left_max ) : left_max = arr [ lo ] else : result += left_max - arr [ lo ] lo += 1 else : if ( arr [ hi ] > right_max ) : right_max = arr [ hi ] else : result += right_max - arr [ hi ] hi -= 1 return result This method employs two pointers (lo and hi, as demonstrated above) that are initialized to point to the first and last elements, respectively. The key is to compute the amount of water that can be held in each element of the array by iterating through it, based on the principle that the water stored in a cell depends on the minimum of max_left[i] and max_right[i] minus array[i]. Both methods effectively address this problem according to the required complexity.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.