Minimum Refueling Stops
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure carrying this problem is a max-heap, and Bloomberg candidates reported Minimum Refuelling Stops in November 2021. If your OA invite is sitting in your inbox, know this: it looks like a DP problem but the clean solution is greedy with a priority queue. The trick is to decide which stations to stop at only after you've run out of fuel. Miss that and you'll burn an hour on a table you don't need. StealthCoder is the safety net if your mind goes blank mid-assessment, but the pattern below should be enough to walk in ready.
The problem
A car starts at position 0 with startFuel units of fuel and must reach position target. It consumes one unit of fuel per unit of distance. Each station is [position, fuel]. On reaching a station, the car may stop once and take all of that station's fuel, or skip it. Return the minimum number of stops needed to reach target, or -1 if the target is unreachable. For this exercise, station positions are distinct and strictly between 0 and target. The input may be unsorted and may be normalized into increasing position order. Function minRefuelStops(target: int, startFuel: int, stations: int[][]) → int Examples Example 1 target = 100 startFuel = 10 stations = [[10,60],[20,30],[30,30],[60,40]] return = 2 Stop at positions 10 and 60. Their fuel is sufficient to reach the target in two stops. Example 2 target = 100 startFuel = 1 stations = [[10,100]] return = -1 The car cannot reach the station at position 10, so no stop is possible. Constraints 1 <= target <= 10^9 0 <= startFuel <= 10^9 0 <= stations.length <= 2 * 10^5 Each station is [position, fuel], with distinct position strictly between 0 and target. 1 <= fuel <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Sort stations by position if they aren't already, then append the target as a final station with zero fuel. Walk through them. For each station, subtract the distance from your current fuel. While fuel is negative, pop the largest fuel amount from the max-heap, add it to your tank, and count one stop. If the heap is empty and fuel is still negative, return -1. Otherwise push this station's fuel onto the heap and continue. You're retroactively choosing the best past stations to have stopped at. Common pitfalls: forgetting to sort the unsorted input, checking fuel only after pushing, and missing the sentinel target station so the last leg never gets validated. Use 64-bit-safe sums since fuel and positions reach 10^9 and stations reach 2*10^5. Complexity is O(n log n). If you freeze on the heap idea during the live OA, StealthCoder can surface it quietly, but know the loop first.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimum Refueling Stops 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as minimum number of refueling stops. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Refueling Stops FAQ
How hard is Minimum Refueling Stops really?+
It's a LeetCode hard, but the greedy heap solution is short, around 15 lines. The difficulty is spotting that you can defer decisions. Once you see the max-heap idea, the code is mechanical. DP works too but is O(n^2) and risks timing out at 2*10^5 stations.
What's the trick to solving it?+
Drive as far as you can, and remember every station you passed. When you run dry, retroactively take fuel from the passed station with the most fuel. A max-heap gives you that in O(log n). Each pop counts as one stop.
Why does the greedy approach work here?+
Stopping at a station only matters when you need the fuel. Taking the largest available pile when stuck minimizes the number of stops, because any other choice leaves you with less range. Swapping a smaller earlier choice for a larger one never hurts.
What edge cases should I test before submitting?+
Test zero stations with startFuel at least target, startFuel of 0, unsorted stations, a first station beyond your range, and a gap before the target. Also check that the target sentinel gets processed so the final leg is validated.
How do I prepare in 48 hours for this OA?+
Write the heap solution from scratch twice. Then trace Example 1 by hand, tracking fuel and heap contents. Practice the sentinel trick and the sort step. Also review one or two other heap-plus-greedy problems so the pattern feels familiar under pressure.