Inventory Revenue Across Supply, Sell, and Return Logs
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this ZipRecruiter OA, reported in October 2023, is treating inventory as a flat count. It's a per-item price book. Supply adds units at a price, sell eats the cheapest units first, and return puts units back at a brand new price. That's a min-heap or sorted map per item, plus careful bulk handling since counts can be large. You've got an invite and maybe two days. Know the shape before you open it. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and reads the problem for you.
The problem
Process string operations: [supply,item,count,price] adds units at a unit price. [sell,item,count] sells a guaranteed available quantity, consuming cheapest units first, and emits the consumed price total. [return,item,count,originalSalePrice,newPrice] restores returned units at newPrice; originalSalePrice identifies the prior sale lot and emits no output. Return sell revenues in order. Function inventoryRevenue(operations: String[][]) → long[] Examples Example 1 operations = [["supply","apple","2","5"],["supply","apple","1","3"],["sell","apple","2"],["return","apple","1","5","4"],["sell","apple","2"]] return = [8,9] The first sale consumes prices 3 and 5; the second consumes 4 and 5. Example 2 operations = [["supply","x","1","7"],["sell","x","1"]] return = [7] The one supplied unit is sold. Constraints 1 <= operations.length <= 100000 Counts and prices are positive integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is one structure per item: a map from item to a min-heap (or sorted map) of price to quantity. Supply pushes (price, count). Sell pops the cheapest lot, takes min(needed, lotCount), adds taken * price to revenue, and pushes back any remainder. Never loop unit by unit. Counts are positive integers with no stated upper bound, so per-unit loops can blow up, and revenue needs a 64-bit long. Return is the quiet trap. It restores count units at newPrice, so push (newPrice, count) into the heap. The originalSalePrice argument only identifies the prior lot and doesn't change what you push. Example 1 confirms it: after returning one unit at 4, the next sale takes 4 and 5. Complexity is O(n log n). If the heap logic gets tangled under pressure, StealthCoder is the hedge on 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.
You can drill Inventory Revenue Across Supply, Sell, and Return Logs 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter 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.
Inventory Revenue Across Supply, Sell, and Return Logs FAQ
What's the core trick in this ZipRecruiter inventory problem?+
Keep a min-heap of (price, quantity) lots for each item. Sells pop the cheapest lot and take as many units as needed from it, pushing back the leftover. Work in lots, not single units, so large counts stay fast.
What does the return operation actually do?+
It adds count units back into that item's heap at newPrice and outputs nothing. The originalSalePrice just identifies the earlier sale. In Example 1, returning one unit at 4 means the next sale consumes 4 and 5, giving 9.
Why do people fail this one on the first attempt?+
Usually they loop one unit at a time, or they push returns back at the original price instead of newPrice. Another common miss is using int for revenue. Use a long for totals and handle lots in bulk.
Do I need to handle a sell with not enough stock?+
No. The statement guarantees the sold quantity is available. You don't need an error path or an empty-heap check beyond normal loop safety, which keeps the sell loop simple.
How should I prep for this in 48 hours?+
Practice a priority-queue problem where you consume the smallest element in bulk and push back remainders. Then hand-trace Example 1 until the supply, sell, and return flow is automatic. Write the per-item map of heaps from memory once.