Karat / VO Coding Interview Problem: Snowy Mountain Crossing

14 Views
No Comments

You are planning a trek across a snowy mountain. On the mountain, it snows in the morning, the snow melts with the sun in the afternoon, and in the evening you can attempt a crossing.

Snow piles up at each location, making that location higher. If it has not snowed at a particular location for 2 days, the snow there starts melting on the afternoon of the second day, at a rate of one unit per day.

You can climb up and down one level while moving to the next position.

The player needs to cross the mountain with the least amount of climbing possible.

The crossing attempts are limited to the days in the forecast because the weather is unpredictable later.

Write a function that, given the base altitude of locations on the mountain and a list of snow forecasts for each day, calculates and returns the best day to perform the crossing as well as the number of climbs needed on that day.

For example, given the initial altitudes: [0, 1, 2, 1]

And the snow forecast for each morning:

[[1, 0, 1, 0],  # On day zero, one unit of snow will fall on positions 0 and 2.
[0, 0, 0, 0],  # On day one, it will not snow.
[1, 1, 0, 2]]  # On day two, two units of snow will fall on position 3, and one unit on positions 0 and 1.

This is the resulting mountain profile each evening, the player is represented by the letter P.

This problem asks you to simulate a mountain whose heights change over time due to daily snowfall and delayed melting, then choose the best forecast day to cross with the minimum climbing cost. The key is to track the height of every position across days, update snow accumulation in the morning, apply melting after two consecutive snowless days at a rate of one unit per day, and evaluate the traversal cost for each day’s profile. A careful array simulation is usually the main technique, with day-by-day comparison to find the optimal crossing day.

END
 0