Get Min Cost of Purchasing Books
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's book purchasing problem showed up in October 2024 OAs, and it's a greedy optimization question. You're given book prices and some discount rule, and you need to minimize total cost. The trap is thinking you need dynamic programming or brute force when a single greedy pass solves it. This is the kind of problem where the right observation cuts complexity in half. StealthCoder can spot the greedy choice instantly if you blank on the approach during the live OA.
Pattern and pitfall
The greedy pattern here is about choosing which books to bundle or discount first. Typically, you want to apply discounts to the most expensive books to maximize savings, then process the remainder. The common mistake is trying every permutation or using DP when the problem rewards a sorted-first, greedy-selection approach. Sort by price descending, apply discounts to the top items in each bundle, then sum the cost. The trick is recognizing that once you sort, the greedy choice at each step (discount the priciest remaining items) is locally and globally optimal. During the live OA, if you freeze on the recurrence relation, StealthCoder reading the problem will feed you the sorted greedy template, which is the real shortcut.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Get Min Cost of Purchasing Books 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
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon 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.
Get Min Cost of Purchasing Books FAQ
Is this a 'sort then iterate' problem or something harder?+
Sort then iterate, with a greedy selection rule baked in. Once sorted by price, the discount or bundling logic becomes obvious. Don't overthink it. If you're writing DP, you've missed the greedy insight.
What's the most common wrong approach candidates attempt?+
Trying to use dynamic programming to explore all possible discount combinations. The problem is solvable in one linear pass after sorting. DP is overkill and a sign you're not seeing the greedy choice.
Does the discount rule change the algorithm or just the cost calculation?+
The discount rule defines what counts as a 'bundle' or group. Understand the rule first. Once you know it, the greedy strategy is almost always 'apply discount to the highest-cost items first' to maximize total savings.
How should I prepare for this in 24-48 hours?+
Review greedy problems involving sorting and discounts or bundles. Practice identifying when a greedy choice is safe. For Amazon specifically, book bundling and discount stacking are common OA themes. One or two problems is enough.
Will this problem have edge cases around zero books or single discounts?+
Almost certainly yes. Handle empty input, single book, and the case where discount doesn't apply. These are quick wins if you think of them early. Don't lose points on boundary conditions.