跪拜 Guibai
← All articles
Interviews · Algorithms · Developers

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

By Fox爱分享 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The puzzle is a compact test of backward induction and recursive reasoning, the same mental model that underpins base-case thinking in algorithm design and equilibrium analysis in distributed systems. An interviewer who watches a candidate work from N=1 upward learns more about their engineering instincts than any answer blurted from memory.

Summary

The setup: N tigers and one sheep are locked together. A tiger can eat the sheep but then turns into a sheep itself, and every tiger's first priority is survival. When N=100, the sheep lives — not despite the tiger count, but because of it. The reasoning chains backward from the smallest cases: one tiger eats, two tigers don't, three eat, four don't. The pattern holds for all N: odd means the sheep is eaten, even means it isn't.

Each tiger runs the same recursive logic — if eating turns the game into an N-1 state where the new sheep dies, no one eats. At N=100, eating creates an N=99 state where the sheep dies, so every tiger abstains. The result is a subgame perfect Nash equilibrium where collective rationality protects the one creature nobody individually wants to protect.

The interviewer wasn't testing for a memorized answer. Walking through N=1, N=2, spotting the parity pattern, and verifying it demonstrates the engineering instinct to shrink a problem to its base case and reason upward — the same muscle used to write a recursive function or debug an off-by-one error.

Takeaways
With 1 tiger, the sheep is eaten because the tiger faces no threat after transforming.
With 2 tigers, neither eats because eating turns the eater into a sheep that the remaining tiger would then kill.
With 3 tigers, eating produces the safe N=2 state for the new sheep, so a tiger will eat.
The pattern generalizes: odd N means the sheep is eaten; even N means the sheep survives.
At N=100, eating creates an N=99 state where the new sheep dies, so no tiger acts and the sheep lives.
The reasoning method is backward induction — starting from the simplest terminal state and working forward.
The equilibrium is a subgame perfect Nash equilibrium: no tiger can improve its outcome by unilaterally changing strategy.
Conclusions

The puzzle's counterintuitive punch is that more tigers make the sheep safer, not more endangered — a one-tiger difference flips the outcome completely, much like an off-by-one bug.

Interviewers value the visible reasoning path far more than a correct answer; walking through N=1, N=2, and N=3 aloud signals the recursive-decomposition habit that distinguishes engineers from trivia collectors.

The same backward-induction structure appears in distributed-systems problems like Byzantine fault tolerance and circuit breaking, where individually rational node decisions produce system-level outcomes no single node intended.

Concepts & terms
Backward induction
A game-theory reasoning method that starts from the final possible state of a sequential game and works backward to determine optimal actions at each earlier step.
Subgame perfect Nash equilibrium
A refinement of Nash equilibrium where players' strategies form a Nash equilibrium in every subgame of the original game, ensuring credibility of threats and promises at every decision point.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗