Goldman Sachs VO Coding Interview: Compute Snowpack Between Hills

21 Views
No Comments

Given an array of non-negative integers representing the elevations from the vertical cross section of a range of hills, determine how many units of snow could be captured between the hills.

See the example array and elevation map below.

[0, 1, 3, 0, 1, 2, 0, 4, 2, 0, 3, 0]

In this example, 13 units of snow (*) could be captured.

Implement computeSnowpack(arr) correctly.

This is a classic trapping-rain-water style problem: given a non-negative elevation array, compute how many units of snow can accumulate between the hills. The standard solution uses either prefix/suffix maximum arrays or a two-pointer sweep to sum, for each position, the water level bounded by the lower of the tallest bars on both sides. The sample input [0, 1, 3, 0, 1, 2, 0, 4, 2, 0, 3, 0] yields 13 units, making it a good test of array traversal, boundary handling, and efficient O(n) reasoning.

END
 0