458. Poor Pigs
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of pigs required to find a single poisonous bucket out of buckets total buckets within minutesToTest time. A pig dies minutesToDie minutes after drinking from the poisonous bucket.
Solution
Poor Pigs in Python
class Solution:
def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
# If there's only one bucket, we don't need any pigs.
if buckets == 1:
return 0
# Calculate how many states one pig can represent.
# A pig can die in any of the test rounds, or it can survive.
# Number of rounds = minutesToTest // minutesToDie.
# Total states = number of rounds + 1 (for survival).
states_per_pig = (minutesToTest // minutesToDie) + 1
# We need to find the minimum number of pigs 'x' such that:
# (states_per_pig)^x >= buckets
pigs = 0
testable_buckets = 1
# Iteratively find the smallest 'pigs' count.
while testable_buckets < buckets:
testable_buckets *= states_per_pig
pigs += 1
return pigs
Poor Pigs in Java
class Solution {
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
// If there's only one bucket, we know it's the poisonous one without any tests.
if (buckets == 1) {
return 0;
}
// Calculate the number of states one pig can represent.
// A pig can die after round 1, round 2, ..., or it can survive.
// Number of possible test rounds = minutesToTest / minutesToDie.
// Total states per pig = (number of rounds) + 1 (for the survival case).
int statesPerPig = minutesToTest / minutesToDie + 1;
// We need to find the minimum number of pigs 'x' such that:
// (statesPerPig)^x >= buckets
int pigs = 0;
long testableBuckets = 1;
// Iteratively find the smallest 'pigs' count.
while (testableBuckets < buckets) {
testableBuckets *= statesPerPig;
pigs++;
}
return pigs;
}
}
Poor Pigs in C++
class Solution {
public:
int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
// If there's only one bucket, we don't need any pigs.
if (buckets == 1) {
return 0;
}
// Calculate how many states one pig can represent.
// A pig can die in any of the test rounds, or it can survive.
// Number of rounds = minutesToTest / minutesToDie.
// Total states = number of rounds + 1 (for survival).
int states_per_pig = minutesToTest / minutesToDie + 1;
// We need to find the minimum number of pigs 'x' such that:
// (states_per_pig)^x >= buckets
int pigs = 0;
long long testable_buckets = 1; // Use long long to avoid potential overflow
// Iteratively find the smallest 'pigs' count.
while (testable_buckets < buckets) {
testable_buckets *= states_per_pig;
pigs++;
}
return pigs;
}
};
Poor Pigs in TypeScript
function poorPigs(buckets: number, minutesToDie: number, minutesToTest: number): number {
// If there's only one bucket, we know it's the poisonous one without any tests.
if (buckets === 1) {
return 0;
}
// Calculate the number of states one pig can represent.
// A pig can die after round 1, round 2, ..., or it can survive.
// Number of possible test rounds = floor(minutesToTest / minutesToDie).
// Total states per pig = (number of rounds) + 1 (for the survival case).
const statesPerPig = Math.floor(minutesToTest / minutesToDie) + 1;
// We need to find the minimum number of pigs 'x' such that:
// (statesPerPig)^x >= buckets
let pigs = 0;
let testableBuckets = 1;
// Iteratively find the smallest 'pigs' count.
while (testableBuckets < buckets) {
testableBuckets *= statesPerPig;
pigs++;
}
return pigs;
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.