AnalystPath

Rainwater Held by a Skyline

SQLHardSenior level~15 min

Problem

A city skyline is described by the table `Skyline`, where each row gives a `position` (left-to-right index) and the `wall_m` height in metres of a unit-wide rooftop ledge at that position. After a storm, water pools on the rooftops: at any position the standing water level equals the shorter of the tallest ledge to its left (inclusive) and the tallest ledge to its right (inclusive); the water depth at that position is that level minus the position's own ledge height. Return a single column `pooled_water` with the total volume of water pooled across the whole skyline.

Tables

Example rows — the live problem includes the full dataset.

Skyline
positionwall_m
10
21
30

Expected output

Your answer should return 1 row with the columns pooled_water.

Starter code (SQL)

SELECT *
FROM Skyline;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions