Shopping Cost
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're staring at a TikTok shopping problem from March 2025, and it's a greedy allocation question dressed up in influencer clothing. Alex has vouchers that halve item prices exponentially, and you need to minimize total cost. The trick is simple: apply vouchers to the highest-priced items first, repeatedly. This is a heap problem. StealthCoder will catch the pattern if you freeze mid-OA, but you should lock in the greedy logic before the assessment starts.
The problem
Alex is a TikTok influencer who loves shopping online and sharing his latest hauls with his followers. He's super excited about buying several products from an e-commerce website to feature in his next video. Each item on his shopping list has its own price, and Alex wants to show his followers how to get the best deals. Alex has a number of discount vouchers that he can use on any of his purchases. These vouchers offer a unique discount mechanism: the more vouchers used on a single item, the greater the discount. For instance, for each voucher used on an item, the price of that item is halved. If multiple vouchers are used on the same item, the price is halved repeatedly. The discount can be represented by the following formula: discounted_price = p/2^k, where: p is the original price of the item, k is the number of vouchers used on that item. Alex needs to figure out the minimum total cost he'll need to pay to buy all the items he wants while using his vouchers as effectively as possible. Can you help Alex make his followers proud with his savvy shopping skills? Function Description Complete the function calculateTikTokShoppingCost in the editor below. calculateTikTokShoppingCost has the following parameter(s): int vouchersCount: the number of discount vouchers. int prices[n]: an array representing the prices of the n items Alex wants to buy. Returns int: the minimum total cost Alex will need to pay to purchase all the items. Constraints 1 ≤ prices.length ≤ 10^5 1 ≤ vouchersCount ≤ 10^5 1 ≤ prices[i] ≤ 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The core insight is that each voucher application halves the price of a single item. To minimize total cost, you always want to apply the next voucher to whichever item currently has the highest price. Build a max-heap from the prices array, then iterate through your voucher count: pop the max, divide it by 2 (integer division), push it back. After all vouchers are spent, sum the heap. The heap ensures O(n + v*log(n)) time complexity where v is voucher count. Most candidates try greedy by sorting once and miss the dynamic reallocation. StealthCoder spots this hedge if you go blank on the heap structure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Shopping Cost 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as minimize deviation in array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shopping Cost FAQ
Is this actually greedy or do I need DP?+
Pure greedy. Use a max-heap. Every voucher goes to the current maximum price. No future state dependency. DP is overkill and will time out on 10^5 items and vouchers.
Should I pre-sort the prices array?+
No. Sorting once doesn't work because prices change after each voucher. You need a dynamic max-heap that re-orders itself. That's why heap, not sort.
What about integer division vs float division?+
Use integer division (floor). Price is an int. p/2 with integers truncates down. If the problem returns an int, your final sum must be int too. No rounding ambiguity.
Can I use the same voucher on the same item twice?+
Yes. The problem says 'if multiple vouchers are used on the same item, the price is halved repeatedly.' Each voucher application is independent. One item can consume all vouchers.
How do I verify my answer on a quick trace?+
Tiny example: prices=[100, 50], vouchers=2. Heap: [100, 50]. Pop 100, divide by 2 = 50, push back. Heap: [50, 50]. Pop 50, divide by 2 = 25, push back. Heap: [50, 25]. Sum = 75. Hand-check against brute force.