The Maximum Ice Cream Bars coding problem gives you an array of ice cream costs and a number of coins. You want to buy as many ice cream bars as possible. There is no constraint on which bars you can buy — just maximize the count. This is a classic greedy problem with a twist on how you implement the sort efficiently.
Amazon and other companies use this problem as a greedy sorting warm-up. While the approach is straightforward (sort and greedily buy cheapest first), the interesting angle is using counting sort to achieve O(n + max_cost) time instead of O(n log n). This tests whether candidates know when to apply non-comparison-based sorting for integer inputs with bounded range.
The optimal approach uses counting sort + greedy. Count the frequency of each price. Iterate from cheapest to most expensive, buying as many of each price as possible (limited by count and remaining coins). This is O(n + max_cost) time and O(max_cost) space — optimal for the given constraints. Alternatively, standard sort + greedy is O(n log n) and also acceptable, but less impressive in interviews.
Costs: [1, 3, 2, 4, 1], coins = 7.
With counting sort: freq[1]=2, freq[2]=1, freq[3]=1, freq[4]=1. Same traversal, same result.
freq[price] bars at a given price.For the Array Sorting Counting Sort Greedy interview pattern, whenever the problem says "maximize count, minimize cost per item," immediately reach for greedy with sort. Mention the counting sort optimization to demonstrate awareness of linear-time sorting alternatives when input values are bounded integers.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Number of Moves to Seat Everyone | Easy | Solve | |
| Array Partition | Easy | Solve | |
| Find Minimum Time to Finish All Jobs II | Medium | Solve | |
| Maximize Happiness of Selected Children | Medium | Solve | |
| Maximum Bags With Full Capacity of Rocks | Medium | Solve |