Get Minimum Fruits
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's December OA included a fruit-crushing game where you repeatedly remove pairs of different fruit types until you can't anymore. The hook is that you're not actually simulating removals. You're finding a mathematical property of the input: the minimum fruits left depends on the frequency distribution, not the order of operations. If one fruit type dominates the array, that's your answer. StealthCoder will help if the math escapes you live.
The problem
Breaking! πππ₯π₯ Amazon has recently introduced an exciting new game called πππLet's Play Crush Fruit Togetherππ₯π₯! In this game, players can select any two distinct types of fruits and crush them together. Each fruit type is represented as a unique integer within an array. Your task is to determine the minimum possible number of fruits remaining after repeatedly performing this operation. That means, as long as there exist at least two different fruit types in the array, you can keep picking and removing them. Function Description Complete the function getMinimumFruits in the editor. getMinimumFruits has the following parameter(s): Returns The function should return a single integer, which represents the smallest number of fruits left in the array after performing the "crush operation" as many times as possible.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is recognizing this is a greedy counting problem, not a simulation. When you crush two different fruits, you reduce the count of each by one. The optimal strategy is to always crush the most frequent fruit with any other fruit. This continues until only one fruit type remains, or the counts balance. The minimum remaining is the absolute difference between the maximum frequency and the sum of all other frequencies, or zero if no type dominates. The common pitfall is trying to simulate or use a heap when you just need to count occurrences and do math. StealthCoder catches this pattern instantly if you freeze.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Get Minimum Fruits 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximum frequency stack. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Minimum Fruits FAQ
Is this actually a simulation problem?+
No. You don't need to track removal order. The answer is determined by frequency counts alone. The fruit type with the highest count will be what's left, if anything. Calculate max frequency minus the sum of all others.
What if multiple fruits have the same high frequency?+
If the max frequency equals the sum of all other frequencies, you can pair them down to zero or one fruit left. If max exceeds the sum, you're left with the difference. Work through the math, not the simulation.
Do I need a priority queue or heap?+
No. Count frequencies in a hash table or dictionary. Find the max. Subtract the sum of the rest. That's your answer. Simulating with a heap is overkill and a red herring.
How do I verify my logic in 2 minutes?+
Test with a simple case: [1, 1, 2]. Max freq is 2. Sum of others is 1. Answer is 2 - 1 = 1 fruit left. Trace it: crush 1 and 2, then crush 1 and 2 again. One 1 remains. Check.
Is this still being asked at Amazon in 2024?+
Frequency and greedy patterns stay relevant. The exact problem may vary, but the underlying pattern (max frequency dominance) is classic. Master the concept, not the fruit metaphor.