Minimum Cost of Buying Candies With Discount
A easy-tier problem at 62% community acceptance, tagged with Array, Greedy, Sorting. Reported in interviews at Garmin and 1 others.
You're buying candies and every third one is free. Sounds simple until you realize the order matters and you're counting down from the most expensive. Garmin and Nokia have both asked this, and it catches candidates who jump straight to a naive loop instead of thinking about which candy should be the free one. The greedy insight is buried in the problem statement, and you'll either spot it in 90 seconds or spend five minutes debugging the wrong approach. If this lands on your OA and you blank, StealthCoder surfaces the sorted solution in seconds, invisible to the proctor.
Companies that ask "Minimum Cost of Buying Candies With Discount"
Minimum Cost of Buying Candies With Discount is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe trap is thinking you iterate through candies in the order given and discount every third. Wrong. The trick is sorting by price in descending order, then every third candy (positions 2, 5, 8, and so on in the sorted list) costs nothing. Greedy works here because you want to maximize the discount value, which means the 'free' slot should go to the most expensive candy in each group of three. Most candidates either skip the sort, or sort ascending instead of descending, or miscount the positions. The algorithm is three lines after the sort. This problem rewards careful reading over algorithmic depth. When you hit this live, StealthCoder runs the sort and indexing logic while you stabilize.
Pattern tags
You know the problem.
Make sure you actually pass it.
Minimum Cost of Buying Candies With Discount recycles across companies for a reason. It's easy-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Cost of Buying Candies With Discount interview FAQ
Is this problem actually easy or did the acceptance rate lie?+
The 62% acceptance rate is honest. Most failures happen because candidates don't sort descending or misunderstand which candies are free. The algorithm itself is trivial once you see the pattern. It's a reading comprehension problem disguised as a coding problem.
How does sorting help here?+
Sorting lets you apply greedy logic: discount the highest-priced candy in each group of three. If you don't sort, you're left guessing which candies the discount applies to. Descending order is critical because you want to waste the free slot on the most expensive item, not the cheapest.
Why is this asked at Garmin and Nokia?+
Both companies use easy problems to filter candidates who skip the problem statement. They're screening for attention and ability to map a real-world scenario (buy two get one free) to an algorithm. It's a quick win if you read carefully, a frustrating miss if you don't.
What's the most common mistake on this one?+
Sorting ascending instead of descending, or forgetting to sort at all. A few candidates also miscount which positions represent every third candy. Writing out positions 0, 1, 2 (free), 3, 4, 5 (free) on paper catches this instantly.
How does this relate to Greedy and Array topics?+
Greedy: you make the locally optimal choice (discount the most expensive) and it yields the global optimum. Array: you're indexing a sorted list and summing specific positions. Neither topic is deep here, but the combination tests whether you can recognize a greedy pattern in plain language.
Want the actual problem statement? View "Minimum Cost of Buying Candies With Discount" on LeetCode →