VM Rental Revenue
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's VM Rental Revenue problem hits you with a deceptively simple rule: customers always rent from the VM type with the most stock left, and each rental's revenue is the sum of the current highest and lowest non-zero stock. You need to track stock across multiple VM types, pick the right one each time, and calculate cumulative revenue. It's a heap problem disguised as a simulation. StealthCoder will catch you if the stock tracking logic gets tangled during the live OA.
The problem
There are multiple VM types, each with an initial stock count. A sequence of customers rent one VM at a time. Each customer always rents from the VM type with the highest remaining stock. The revenue from a rental equals the sum of the current highest stock and the current lowest non-zero stock across all VM types. After the rental, the chosen VM type loses one unit of stock. Return the total revenue after serving customerRequests customers. The rental costs are 5, 4, 3, and 3, for a total revenue of 15.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trap is thinking you can precompute or sort once. You can't. Every rental changes the stock distribution, so you need a data structure that lets you find the max and min efficiently after each decrement. A max-heap for the highest stock and a way to track the current minimum across all non-zero stocks is the move. Many candidates try to use a simple array and rescan every iteration, which works but tanks on large inputs. The real insight is that you're maintaining two extremes (max and min) while one of them shrinks by one each round. Heap operations keep this O(n log m) where m is the number of VM types. StealthCoder will surface this pattern instantly if you freeze on the simulation logic.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill VM Rental Revenue 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
VM Rental Revenue FAQ
Do I need to handle the 'lowest non-zero stock' rule carefully?+
Yes. After each rental, a VM type loses stock. If it hits zero, it's no longer in the pool for the minimum calculation. You can't just track a global min across all types. Filter out zeros or maintain a secondary structure to skip them when finding the minimum.
Is a max-heap enough, or do I need both a min-heap and max-heap?+
A max-heap tracks the highest stock efficiently, but you also need a way to find the lowest non-zero stock. Some solutions use a max-heap plus a set or priority queue for minimums. Others maintain the heap carefully and query it selectively. The bottleneck is the minimum lookup.
What's the most common mistake on this problem?+
Forgetting to update stock counts correctly or recalculating min/max naively on every iteration. Also, candidates sometimes include zero-stock VMs in the minimum calculation, which breaks the revenue formula. Test with the given example (5, 4, 3, 3 revenue chain).
Can I solve this with just a sorted array or list?+
Technically yes, but it's slower. Sorting or binary search after each decrement is O(m log m) per rental, which compounds to O(n*m log m) overall. A heap-based approach is O(n log m), cleaner, and what Amazon expects to see.
How much preparation time do I really need for this?+
If you see 'heap' and 'track min/max', you're halfway there. The simulation itself is straightforward once the data structure is in place. 15-20 minutes to code and trace through the example. Don't overthink the stock decrement logic.