Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Trap Rain Water Between Bars

HardPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Trap Rain Water Between Bars. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

You need to log in / sign up to run or submit.

Problem

In a Meta-style coding interview, you are given an array height where each element represents the height of a vertical bar in an elevation map. Return the total amount of rain water that can be trapped after raining.

Formal Specification

  • Input: height, a list of non-negative integers
  • Output: An integer representing the total units of trapped water

Water above index i is determined by the shorter of the tallest bar to its left and right, minus height[i], if that value is positive.

Constraints

  • 1 <= len(height) <= 2 * 10^4
  • 0 <= height[i] <= 10^5
  • Return the total trapped water as an integer

Function Signature

def trap_rain_water(height):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output