Poor Pigs

HardMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

There are buckets buckets of liquid, and **exactly one** is poisoned. You have pigs to find it and minutesToTest minutes in total. A test round works like this: pick any set of live pigs, let each drink from any subset of buckets (instantly, drinking from many buckets at once is allowed), then wait minutesToDie minutes. Any pig that drank the poison dies at the end of the wait; the others survive and can be reused. You can run rounds back to back for as long as the total time allows (at most floor(minutesToTest / minutesToDie) rounds). Return the **minimum number of pigs** needed to guarantee identifying the poisoned bucket within the time limit. With a single bucket no pigs are needed.

Examples

Input: buckets = 9, minutesToDie = 10, minutesToTest = 20
Output: 2
There are 2 rounds, so each pig has 3 outcomes: dies in round 1, dies in round 2, or survives. Two pigs give 3 * 3 = 9 outcomes, one per bucket.
Input: buckets = 1000, minutesToDie = 15, minutesToTest = 60
Output: 5
4 rounds give 5 outcomes per pig. 5^4 = 625 < 1000, but 5^5 = 3125 >= 1000.

Constraints

  • `1 <= buckets <= 1000`
  • `1 <= minutesToDie <= minutesToTest <= 100`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems