The Maximum Tastiness of Candy Basket problem asks you to pick k candies from a set of candies, each with a given price. The "tastiness" of a chosen basket is defined as the minimum absolute difference between the prices of any two candies in that basket. Your goal is to pick k candies such that this tastiness value is as large as possible. It is a classic "maximize the minimum" problem.
This "Maximum Tastiness of Candy Basket interview question" is popular at companies like Amazon, eBay, and PhonePe. It tests whether a candidate can recognize the "Binary Search on Answer" pattern. This pattern is essential for problems where finding the answer directly is hard, but checking if a specific answer is "possible" is relatively easy. It evaluates your ability to combine sorting, binary search, and greedy validation.
The "Array, Sorting, Binary Search, Greedy interview pattern" is used here.
mid:
k candies such that every pair has a difference >= mid?mid away from the previous one, and so on.k candies, mid is possible.Prices: [1, 3, 21, 5, 8], k = 3
One major mistake in the "Maximum Tastiness of Candy Basket coding problem" is trying to use dynamic programming or a nested loop to find the candies, which would be extremely slow. Another error is not sorting the array before starting the greedy check—sorting is what allows the greedy "pick next possible" strategy to work correctly. Candidates also sometimes get the binary search conditions wrong (e.g., when to move the low vs high pointers).
Memorize the "Maximize the Minimum" trigger. Whenever a problem asks you to maximize the minimum distance/difference/value, it is almost certainly a Binary Search on Answer problem. Get very comfortable with the greedy isPossible function, as that is the engine that drives the binary search. Being able to explain why sorting the input helps the greedy check will demonstrate a deep understanding of the algorithm's mechanics.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximize Score of Numbers in Ranges | Medium | Solve | |
| Maximum Number of Integers to Choose From a Range II | Medium | Solve | |
| Maximum Running Time of N Computers | Hard | Solve | |
| Valid Triangle Number | Medium | Solve | |
| Maximum Number of Integers to Choose From a Range I | Medium | Solve |