Souvenir Shop Purchases
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon Souvenir Shop question from June 2026 looks like a simple simulation, but it's really a math problem hiding behind a loop. Prices climb every time you buy an item, so a naive walk through the shelf blows up when m is huge. If you've got an OA invite for Amazon, expect this one or a close cousin. The trick is counting full laps in bulk instead of buying one item at a time. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea here is short enough to own tonight.
The problem
In an Amazon Souvenir Shop, a shopper visited a souvenir shop with items arranged on the shelf from left to right. The goal is to purchase as many items as possible within a given budget. Notably, the cost of each souvenir increases with each purchase. Formally, given an array cost of size n, representing the initial cost of each item in the souvenir shop, and m representing the initial amount of money that the shopper has. The first time a souvenir is bought its cost will be cost[i], the second time it will be 2 * cost[i], the third time it will be 3 * cost[i], and the jth time it will cost j * cost[i], and so on. The shopper will buy items one by one from left to right, and when she reaches the last item she will go back to the start and repeat this operation until she runs out of money. What is the number of items that the shopper will buy before she runs out of money? Function countPurchasedItems(cost: int[], m: long) → long Examples Example 1 cost = [2, 5, 1, 1] m = 18 return = 5 Assuming 1-based indexing of the cost array: Buy item 1 for 2. Remaining money: 18 - 2 = 16. New costs: [4, 5, 1, 1]. Buy item 2 for 5. Remaining money: 16 - 5 = 11. New costs: [4, 10, 1, 1]. Buy item 3 for 1. Remaining money: 11 - 1 = 10. New costs: [4, 10, 2, 1]. Buy item 4 for 1. Remaining money: 10 - 1 = 9. New costs: [4, 10, 2, 2]. Buy item 1 for 4. Remaining money: 9 - 4 = 5. New costs: [6, 10, 2, 2]. The next item would be item 2, which now costs 10, but only 5 money remains. Therefore, the shopper buys 5 items. Source note (June 28, 2026): The original source image did not include the output for this example. FastPrep worked it out from the problem statement and the given input. If you have the original output or notice something wrong, please let us know and we'll fix it. If we find a fuller source later, we'll come back and update this too. 🐥
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's what it reduces to. Item i, bought j times, costs cost[i] * j. After k full laps, the total spent on item i is cost[i] * (1 + 2 +... + k) = cost[i] * k(k+1)/2. So one lap count k costs S(k) = sum(cost) * k(k+1)/2. That's monotonic in k, so binary search the largest k where S(k) <= m. Then you've bought k * n items. Subtract S(k) from m and simulate one more partial lap, where item i now costs cost[i] * (k+1). Buy left to right until the next item doesn't fit. Pitfalls: overflow when m is a long and k is big, so use 64-bit carefully or cap the search bound. Also stop at the first item you can't afford, don't skip it. If you freeze on the OA, StealthCoder is the hedge that hands you the binary search plus partial lap structure live.
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 Souvenir Shop Purchases 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 Amazon's OA.
Amazon 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.
Souvenir Shop Purchases FAQ
What's the trick in the Amazon Souvenir Shop problem?+
Don't simulate every purchase. The cost after k full laps has a closed form: sum(cost) * k(k+1)/2. Binary search the largest full lap count that fits in the budget, then simulate only the final partial lap. That turns a potentially enormous loop into O(log m + n).
How hard is this one really?+
Medium. The statement is wordy but the math is small. If you spot the triangular number sum, it's maybe 25 lines. The difficulty is noticing that brute force will time out and then handling the partial lap correctly without off-by-one errors.
What edge cases should I test?+
Test m smaller than the first item's cost, which should return 0. Test a single-item array, where you buy repeatedly at costs c, 2c, 3c. Test huge m for overflow. Test when a full lap fits exactly with zero money left, so the partial lap buys nothing.
Does the shopper skip an item she can't afford?+
No. She goes left to right and stops as soon as the next item costs more than the remaining money. Per the Example 1, item 2 costs 10 with only 5 left, so she ends at 5 items even if a later cheaper item exists.
How do I prepare for this in 48 hours?+
Practice binary search on the answer and closed-form sums like n(n+1)/2. Write this problem once from scratch, including the partial lap. Then trace Example 1 by hand: cost [2,5,1,1], m 18, answer 5. If that trace matches your code, you're ready.