The Rabbits in Forest problem asks: in a forest, each rabbit reports how many other rabbits have the same color. Given these answers, find the minimum number of rabbits in the forest. The key insight: if k rabbits answer "x", they may or may not all be the same color — but each group of (x+1) same-colored rabbits produces exactly the same answer x. This coding problem uses a hash map with grouping math. The array, math, hash table, and greedy interview pattern is the core.
Apple, Microsoft, Meta, Amazon, Google, and Bloomberg ask this because the minimum-count reasoning requires careful mathematical thinking: rabbits with answer x must form groups of size (x+1). If you have k such rabbits, you need ceil(k/(x+1)) distinct groups, each contributing (x+1) rabbits.
Frequency count + ceiling division. Count frequency of each answer. For answer x with frequency f: number of groups needed = ceil(f / (x+1)). Total rabbits = sum(groups * (x+1)) for each distinct answer.
answers=[1,1,2]. Frequencies: {1:2, 2:1}.
answers=[10,10,10]: all say 10. Group size = 11. 3 rabbits, one group of 11 suffices. Total = 11.
Rabbits in Forest requires converting a counting problem into a grouping math problem. The formula ceil(count / group_size) appears in many bin-packing and grouping problems. Practice similar problems: "minimum number of groups of size k," "minimum containers needed." The key mathematical step is recognizing that each rabbit reporting "x" belongs to a group of size x+1.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Smallest Missing Non-negative Integer After Operations | Medium | Solve | |
| Group the People Given the Group Size They Belong To | Medium | Solve | |
| Maximum Size of a Set After Removals | Medium | Solve | |
| Number of Boomerangs | Medium | Solve | |
| Minimum Number of People to Teach | Medium | Solve |