跪拜 Guibai
← Back to the summary

100 Tigers, 1 Sheep, and the Off-by-One Error That Saves a Life

Last week a friend came to me and said he had interviewed for a backend development position in ByteDance's advertising and transactions group. He passed the first round. In the second round, the conversation was going well — they talked about project experience and system design — when the interviewer suddenly changed tack and said, "Let me give you a brain teaser."

The question went like this.

N tigers and 1 sheep are locked in together.

The rules are as follows.

Tigers can eat grass, but they prefer to eat sheep. Only one tiger can eat the sheep at a time. After eating the sheep, that tiger turns into a sheep. All tigers are extremely intelligent, perfectly rational, and their first priority is to ensure their own survival.

The question: when N=100, will the sheep be eaten?

My friend said he froze for about two or three seconds.

It wasn't that he had no idea at all — his brain just couldn't turn the corner fast enough. 100 tigers, 1 sheep. The question sounds like the sheep is definitely done for.

But the answer his intuition gave him is often the wrong one.


I later worked through it myself and realized the beauty of this problem isn't in the answer itself, but in the feeling of peeling back layer by layer during the reasoning process.

Let's start with the simplest case.

N=1, only 1 tiger, 1 sheep.

Does the tiger eat?

Yes.

After eating, it turns into a sheep, but now there are no other tigers, so this new sheep can live peacefully eating grass, with no threat at all.

So for N=1, the sheep gets eaten.


N=2, 2 tigers, 1 sheep.

Things start to get interesting.

Suppose Tiger A gets the idea to eat the sheep. It thinks: if I eat the sheep, I turn into a sheep, but Tiger B is still here — won't I, the new sheep, get eaten by Tiger B?

Tiger B is thinking the same thing.

So, if any tiger eats the sheep, it immediately becomes prey in the eyes of the remaining tiger.

A smart tiger wouldn't do that.

The two tigers exchange a glance, tacitly turn around, and go munch on grass.

The sheep is not eaten.


N=3, 3 tigers, 1 sheep.

Suppose Tiger A eats the sheep and turns into a sheep. Now the situation is 2 tigers + 1 new sheep.

This is the N=2 case, and we just deduced that when N=2, the sheep won't be eaten.

That means after Tiger A eats the sheep, the new sheep it becomes is safe.

If it's safe, why not?

Tiger A will eat. Tiger B and Tiger C will think the same way.

N=3, the sheep gets eaten.


N=4, 4 tigers, 1 sheep.

Tiger A eats the sheep, turns into a sheep, and the situation becomes 3 tigers + 1 new sheep — the N=3 case.

When N=3, the sheep gets eaten.

That means after Tiger A turns into the new sheep, it will get eaten by the other tigers.

So it doesn't eat. The other three tigers follow exactly the same calculation logic.

The sheep is not eaten.


By this point, you've probably spotted the pattern.

Odd number of tigers: the sheep gets eaten.

Even number of tigers: the sheep does not get eaten.

Odd, eaten. Even, not eaten.

The pattern is that simple.

But wait — if you keep increasing N, N=5, N=6, N=7, N=8, does the pattern hold?

It holds.

Because when each tiger makes its decision, it runs the same recursive logic in its head: if I eat the sheep, the next state is N-1 tigers plus 1 sheep — is that state safe?

If safe, eat. If not safe, don't eat.

This way of thinking — reasoning backward from the final state — has a name in game theory: backward induction.


Alright, now back to the interviewer's question: N=100.

100 is even.

If any tiger eats the sheep, what remains is 99 tigers plus the new sheep it becomes.

N=99, odd, the sheep will be eaten.

That means after it turns into that sheep, it will get eaten itself.

So no tiger will make a move.

All 100 tigers choose to eat grass.

And that sheep, right in the middle of 100 tigers eyeing it hungrily, lives on, safe and sound.


At this point, I can't help but emphasize how counterintuitive this is.

100 tigers.

Your gut tells you the sheep can't survive, right?

But after working through the logic, you find: the more tigers there are, the safer the sheep becomes.

N=1, sheep dies.

N=2, sheep lives.

N=3, sheep dies.

N=4, sheep lives.

N=5, sheep dies.

N=6, sheep lives.

Do you see the pattern? As long as N is even, the sheep lives, and this pattern holds for all larger even numbers — it doesn't suddenly break just because there are more tigers.

99 tigers, sheep dies.

100 tigers, sheep lives.

A difference of just one tiger, and the fate flips completely.

It feels like writing a line of code with an off-by-one error — the entire system behavior becomes completely different.

Whoa.


What's really behind this problem?

I think the interviewer isn't testing whether you know the answer to this classic puzzle, but whether, when faced with a seemingly complex problem, you instinctively find the simplest starting point.

Many people, upon getting this problem, immediately try to analyze the N=100 case directly. 100 tigers — their brain just explodes.

But the correct approach is to first shrink the problem to the smallest scale, N=1, N=2, see what happens, then find the pattern, and then prove the pattern.

This is mathematical induction, and it's also the most familiar way of thinking for programmers.

When you write a recursive function, don't you first write the base case, then the recurrence relation?

Honestly, this problem, stripped down to its core, is just a recursive function.

willSheepBeEaten(N):
  if N == 1: return true
  if N == 2: return false
  return !willSheepBeEaten(N-1)

Three lines. The entire core logic is right there.


Thinking one layer deeper, this problem is also testing your intuition for "equilibrium."

All 100 tigers not eating the sheep is a Nash equilibrium — more precisely, a subgame perfect Nash equilibrium.

It means that in this equilibrium state, no single tiger can obtain a better outcome (survival) by unilaterally changing its strategy (eating the sheep).

All tigers are rational, and all tigers know that all other tigers are rational. This shared understanding itself ends up protecting a sheep that no one individually wants to protect.

This idea — "everyone's rational choices added together produce an outcome no one expected" — has parallels in backend systems too.

For example, Byzantine fault tolerance in distributed systems: each node isn't sure whether other nodes are sending erroneous messages, but precisely because no one is sure, the entire system reaches consensus through a voting mechanism.

Or consider circuit breaking in microservice architectures: each service "rationally" chooses to disconnect when it can't handle the load, which in turn prevents a cascading failure across the entire system.

The rational decision of a single node produces a counterintuitive result at the system level.

This problem, to some extent, is testing whether you have that intuition — the ability to jump from individual decisions to system-level thinking.


Back to my friend.

He didn't end up getting the offer.

But he said that after he came out, he thought about the problem again and felt the most interesting part wasn't the answer, but how the interviewer kept observing his reasoning path as he worked through it.

First deduce N=1, then N=2, then say "I think I see a pattern," then state the parity conclusion, and finally explain why.

Demonstrating that entire reasoning path is worth far more than just blurting out the answer "even means no."


So, if you encounter this problem in an interview.

Don't rush to give the answer.

You can first tell the interviewer, "Let me think through the simplest cases first," and then start from N=1, step by step, walking the interviewer through your reasoning.

When you reach N=3, you can pause and say, "I think a pattern is emerging — odd means eaten, even means not eaten. Let me verify with N=4." After verifying, then state the conclusion.

What the interviewer wants to see isn't whether you're right, but whether you have that engineering mindset — the ability to break a complex problem down into its smallest units and reason step by step.

That's the truly valuable part of this problem.


100 tigers, 1 sheep, and the sheep lives perfectly fine.

I still find this problem wonderfully elegant. Its brilliance lies in using a seemingly absurd scenario to pack in hardcore concepts like backward induction, Nash equilibrium, and systems thinking.

And it also carries a hidden gentleness.

Sometimes, the best protection isn't some hero coming to the rescue, but a system where every participant is smart enough that no one dares to make the first move.

Pretty interesting when you think about it.


If this piece sparked something for you, feel free to follow "Fox爱分享" for more in-depth analysis of technical interviews. See you in the next one.

Article first published at

Comments

Top 1 from juejin.cn, machine-translated. The original thread is authoritative.

Sorry的芮

Kind of interesting