Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Trapped rainwater

    Trapped rainwater

    Hardarraytwo pointersdynamic programmingstack

    heights[i] is the height of a bar of width 1. After rain, water is trapped between taller bars. Return how much water is trapped.

    Example 1

    Input:  heights = [0,1,0,2,1,0,1,3,2,1,2,1]
    Output: 6

    Example 2

    Input:  heights = [4,2,0,3,2,5]
    Output: 9

    Constraints

    • 0 ≤ heights.length ≤ 2·10⁴
    • 0 ≤ heights[i] ≤ 10⁵

    Submit checks your solution against 8 tests, including hidden ones.

    /**
     * @param {int[]} heights
     * @return {int}
     */
    function trap(heights) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.