Calculate trapped water between elevation bars
TeslaSoftware Engineer · Technical ScreenLast updated
This question evaluates array manipulation, algorithmic optimization, and complexity analysis, specifically the ability to reason about time and space trade-offs and design a linear-time, constant-space approach for computing trapped water.
Given an array of non-negative integers representing the heights of unit-width vertical bars, compute the total water retained after rainfall. Start by presenting a brute-force approach and analyze its time and space complexity. Then optimize to a linear-time solution that uses constant extra space, explain the intuition, and implement the final method.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.