Find the fewest rabbits consistent with survey answers about same-color rabbits

Quick Overview

Surveyed rabbits each report how many other rabbits share their color, and the task is to find the fewest rabbits the whole forest could hold. It tests frequency counting, reasoning about fixed group sizes, and a lower-bound argument for a counting problem.

Find the fewest rabbits consistent with survey answers about same-color rabbits

Company: Zepto

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A forest contains an unknown number of rabbits, and every rabbit has some color. Several distinct rabbits were surveyed. Each one was asked how many other rabbits have the same color as itself. The answers are stored in an integer array `answers`, where `answers[i]` is the reply of the `i`-th surveyed rabbit. Return the smallest possible total number of rabbits in the forest that is consistent with all answers, including rabbits that were not surveyed. Rabbits that give the same answer do not have to share a color. If more rabbits give the same answer than one color group can hold, they must belong to different color groups. ### Function Signature ```python def num_rabbits(answers: list[int]) -> int: ``` ### Rules - Every answer is truthful and counts rabbits in the whole forest, surveyed or not. - The surveyed rabbit does not count itself in its answer. ### Constraints - `1 <= answers.length <= 1000` - `0 <= answers[i] <= 999` ### Examples ```text Input: answers = [1,1,2] Output: 5 ``` Both rabbits that answered 1 can share a color group of size 2, and that group is complete. The rabbit that answered 2 is in a group of size 3, so 2 of those rabbits were not surveyed, for a total of 2 + 3 = 5. ```text Input: answers = [0,0,3,3,3,3,3] Output: 10 ``` Each rabbit that answered 0 is the only rabbit of its color, which gives 2 rabbits. A group for answer 3 holds only 4 rabbits, so the five rabbits that answered 3 need 2 groups (8 rabbits, 3 of them unsurveyed), for a total of 2 + 8 = 10.

Overview: Surveyed rabbits each report how many other rabbits share their color, and the task is to find the fewest rabbits the whole forest could hold. It tests frequency counting, reasoning about fixed group sizes, and a lower-bound argument for a counting problem.

Zepto logo
Zepto
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A forest contains an unknown number of rabbits, and every rabbit has some color. Several distinct rabbits were surveyed. Each one was asked how many other rabbits have the same color as itself. The answers are stored in an integer array answers, where answers[i] is the reply of the i-th surveyed rabbit.

Return the smallest possible total number of rabbits in the forest that is consistent with all answers, including rabbits that were not surveyed.

Rabbits that give the same answer do not have to share a color. If more rabbits give the same answer than one color group can hold, they must belong to different color groups.

Function Signature

def num_rabbits(answers: list[int]) -> int:

Rules

  • Every answer is truthful and counts rabbits in the whole forest, surveyed or not.
  • The surveyed rabbit does not count itself in its answer.

Constraints

  • 1 <= answers.length <= 1000
  • 0 <= answers[i] <= 999

Examples

Input: answers = [1,1,2]
Output: 5

Both rabbits that answered 1 can share a color group of size 2, and that group is complete. The rabbit that answered 2 is in a group of size 3, so 2 of those rabbits were not surveyed, for a total of 2 + 3 = 5.

Input: answers = [0,0,3,3,3,3,3]
Output: 10

Each rabbit that answered 0 is the only rabbit of its color, which gives 2 rabbits. A group for answer 3 holds only 4 rabbits, so the five rabbits that answered 3 need 2 groups (8 rabbits, 3 of them unsurveyed), for a total of 2 + 8 = 10.