10 Mice, 1000 Bottles, and the Binary Trick That Exposes CRUD-Only Backend Engineers
A few days ago, a buddy went to a big tech company for an interview and came back to complain to me: "Fox, ByteDance's second round completely stumped me! Among 1000 bottles of wine, one is mixed with poison. You use mice to test for the poison, and a mouse dies 1 hour after drinking the poison. What's the minimum number of mice needed to find the poisoned bottle? My mind went blank and I blurted out 20. The interviewer just smiled, said nothing, and directly told me to go home and wait for a notification..."
Actually, the correct answer to this question is: 10.
Many people, upon hearing this number, react with: That's impossible! How can 10 mice possibly enumerate the states of 1000 bottles of wine? If you are still stuck in the traditional thinking of "one mouse drinks one bottle," then in front of this classic interview question about high concurrency and high compression, you can only be "dimensionally struck" by the interviewer. Today, Fox will take everyone through the programmer's underlying logic (binary and bitwise operations) to thoroughly and clearly understand this question!
Core Pain Point: Why is it so hard to think of 10?
Most people get stuck on this question because they fall into "assembly-line thinking"—always wanting to let mice try one after another, or to use the mice's lives to fill the gap.
But what the big tech interviewer is testing is not a brain teaser at all, but the essence of Information Theory and computer's underlying storage:
- The state of a mouse: There are only two, either dead (1) or alive (0).
- The essence of the poison: Among 1000 bottles of wine, determine a unique number.
Isn't this just natural binary?
One mouse can express 2 states (0 or 1), two mice can express 2×2=4 states. So, how many states can a combination of 10 mice express?
1024 is greater than 1000! This means that the combination of life-and-death states from 10 mice can completely cover all the numbers of the 1000 bottles of wine!
Dimensional Strike: The "Distributed High-Concurrency" Drinking Method for 10 Mice
Understanding the principle, how do you actually operate it? Do you just make the mice drink a mixture of hundreds of bottles? The mice would die of alcohol poisoning before the poison gets them.
The truly hardcore operation is: "Micro-sampling + Labeled Parallel Testing"!
Step 1: Label the Wine with Binary Tags
We number the 1000 bottles of wine from 1 to 1000 and convert them into 10-digit binary numbers:
- Bottle 1:
00 0000 0001 - Bottle 2:
00 0000 0010 - Bottle 3:
00 0000 0011 - ...
- Bottle 1000:
11 1110 1000
Step 2: Prepare 10 "Dedicated Test Pools"
We prepare 10 test pools, corresponding to the 1st to 10th bits of the binary number.
The rule is very simple: Look at the wine's binary number. If the nth bit is 1, take a drop from this bottle and put it into the nth test pool!
- Bottle 1 (
00 0000 0001): Only the 1st bit is 1, take a drop and put it only into Pool 1. - Bottle 2 (
00 0000 0010): Only the 2nd bit is 1, take a drop and put it only into Pool 2. - Bottle 3 (
00 0000 0011): The 1st and 2nd bits are both 1, take two drops and put them into Pool 1 and Pool 2 respectively. - ...
- Bottle 1000 (
11 1110 1000): The 4th, 6th, 7th, 8th, 9th, and 10th bits are 1, take samples and put them into the corresponding 6 pools.
After the distribution is complete, each pool contains a mixture of micro-samples from hundreds of bottles of wine, but they are isolated from each other.
Step 3: High-Concurrency Parallel Poison Testing
We bring out 10 mouse tool-men and line them up. Let mouse n drink from the corresponding test pool n!
Mouse10 (drinks Pool 10) ... Mouse3 (drinks Pool 3) Mouse2 (drinks Pool 2) Mouse1 (drinks Pool 1)
Since the 10 mice drink from their respective pools simultaneously (high concurrency), one round of testing handles everything!
The Final Blow: Check the Mice's Fate to Pinpoint the Poison
After drinking, wait quietly for the poison to take effect (assuming the poison has a fixed incubation period). Next is the moment to witness a miracle.
After the results are out, we record a dead mouse as 1 and a live mouse as 0. Arrange the states of the 10 mice in order to form a binary number:
- If only Mouse 1 died, and the others are alive: The state is
00 0000 0001→ Converted to decimal is 1. Bottle 1 is the poisoned wine! - If Mouse 1 and Mouse 2 died, and the others are alive: The state is
00 0000 0011→ Converted to decimal is 3. Bottle 3 is the poisoned wine! - If Mice 4, 6, 7, 8, 9, 10 died, and the others are alive: The state is
11 1110 1000→ Converted to decimal is 1000. Bottle 1000 is the poisoned wine!
See that? The death combination of the mice is the absolute address (pointer) of the poison! No extra mice are needed; the life-and-death states of 10 mice perfectly map to every corner of the 1000 bottles of wine.
Fox's Technical Summary: What Are Big Tech Companies Really Testing?
This question seems like a logic brain teaser on the surface, but in the eyes of an architect, it maps to extremely core backend technical ideas:
- Bitmap and Bloom Filter: Using extremely small space (10 bits) to express the state of massive data. In massive data deduplication and Redis cache penetration defense, Bloom filters use this "many-to-one mapping" bitwise operation idea.
- Space for Time vs. Time for Space: If you use 1 mouse to poll and drink 1000 times, that's trading time for space, which big tech companies can't afford to wait for; the interviewer wants efficiency, 10 mice solve it in one go, which is the ultimate compression of state space.
- The Underlying Power of Binary: Many so-called backend experts who have worked for 5 or 6 years are stuck in business CRUD day after day and can't even figure out bitwise operations like
&,|,^,<<. This question is a touchstone that instantly reveals your sensitivity to the analysis of underlying computer principles.
Next time you go to a big tech company for an interview and encounter this kind of problem, don't panic and start counting. Remember, in the world of computers, everything can be binary!
Welcome to follow 'Fox loves to share', using the most hardcore technology to help you dimensionally strike every interview! Like, Wow, and Forward, brothers, get on it!
Top 2 of 6 from juejin.cn, machine-translated. The original thread is authoritative.
I don't get it. One mouse is clearly enough. Just let one mouse keep testing the poison. It'll die when it drinks the poisoned one. There's only one poisoned bottle, so once it hits it, you just eliminate that one. It's a bit like asking a PhD whether rain falling from ten thousand meters would kill someone. A bunch of PhDs are busy calculating, but they're outdone by a cleaning lady asking, "Haven't you ever been rained on?"
Don't ask me if the efficiency would be terrible, because I feel sorry for the mouse [innocent-dumb]
Haha, this kind of problem should also have time or attempt limits, otherwise the question is actually meaningless
Awesome