Reported February 2025
Zipheap priority queue

Furthest Building with Sandbags and Ropes

Reported by candidates from Zip's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Zip OA. Under 2s to a working solution.
Founder's read

Zip reported this one in February 2025, and the detail that matters is that a rope covers any climb, no matter how tall. That's the whole problem. You walk a row of buildings, pay for upward moves with sandbags or ropes, and return the furthest index you can reach. It's the classic Furthest Building problem with renamed resources. The pattern is a greedy choice backed by a min-heap. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you the approach in real time. Better to know the trick before you open the invite.

The problem

You travel through a row of buildings in order, starting at index 0. Moving to a building of equal or lower height is free. For an upward move of d height units, spend either d sandbags or one rope. A rope covers any upward height difference.
Given heights, the number of available sandbags, and the number of available ropes, return the largest building index you can reach. You may choose which climbs use ropes as you travel.

Function
furthestBuilding(heights: int[], sandbags: int, ropes: int) → int

Examples
Example 1
heights = [4,2,7,6,9,14,12]
sandbags = 5
ropes = 1
return = 4
Use five sandbags for the climb from 2 to 7 and a rope for the climb from 6 to 9. The next uphill move cannot be paid for.
Example 2
heights = [2,6,7]
sandbags = 1
ropes = 1
return = 2
Use the rope for the climb of four and one sandbag for the final climb.
Example 3
heights = [1,5,2,6]
sandbags = 4
ropes = 0
return = 2
After spending four sandbags on the first climb, no resource remains for the final climb.

Constraints
1 <= heights.length <= 100000.
1 <= heights[i] <= 1000000.
0 <= sandbags <= 1000000000.
0 <= ropes <= heights.length.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: don't decide up front which climb gets a rope. Walk left to right and push every positive climb into a min-heap. Pay for it with sandbags by subtracting the climb from your sandbag count. When sandbags go negative, pop the smallest climb from the heap and give it a rope instead, refunding those sandbags. If ropes run out and you're still negative, return the previous index. The reasoning is that ropes belong on the biggest climbs, and the heap lets you retroactively swap. Common pitfall: greedily spending sandbags first without the refund step, or using ropes on the first climbs you see. Another is forgetting that flat or downhill moves cost nothing. Complexity is O(n log n) time and O(n) space, which fits 100000 buildings easily. If the heap logic slips under pressure, StealthCoder is the hedge during the live OA.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Furthest Building with Sandbags and Ropes cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as furthest building you can reach. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Zip's OA.

Zip reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Furthest Building with Sandbags and Ropes FAQ

What's the trick for Furthest Building with Sandbags and Ropes?+

Use a min-heap of climbs you've paid for. Spend sandbags on every climb first. When you run short, take the smallest climb in the heap, convert it to a rope, and refund its sandbags. Ropes end up on the biggest climbs automatically.

How hard is this problem really?+

It's medium, but the greedy swap idea is what trips people up. Once you see that you can reassign a rope retroactively, the code is about fifteen lines. Brute force over rope placement explodes, so don't bother with it.

Why not just use ropes on the biggest climbs directly?+

You don't know which climbs are biggest until you've seen the whole path, and you need the furthest reachable index. The heap lets you decide lazily as you go, correcting earlier choices only when resources run out.

What edge cases should I test?+

Test zero ropes, as in Example 3, where sandbags alone decide the answer. Test a single building, which returns 0. Test all-descending heights, which cost nothing and return the last index. Also test sandbags up to 1000000000 so you don't overflow smaller types.

How do I prepare for this in 48 hours?+

Write the heap solution once from memory, then trace Example 1 by hand. Learn the pattern of paying first and fixing later with a heap. It shows up in many scheduling and resource problems, so it's worth the hour.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Zip.

OA at Zip?
Invisible during screen share
Get it