In the Stone Game coding problem, two players, Alice and Bob, play a game with an even number of piles of stones arranged in a row. Each pile has a positive number of stones. The players take turns taking all the stones from either the first or the last pile in the current row. Alice goes first. The goal is to determine if Alice can win (collect more stones than Bob).
This is a famous problem that tests both Dynamic Programming and mathematical observation. While it can be solved with DP, there's a mathematical "trick" that guarantees the first player always wins under specific conditions (even number of piles, total stones is odd). Companies like Meta and Google ask this to see if you can provide the formal DP solution while also being observant enough to find the constant-time mathematical proof.
The formal Algorithmic pattern is Dynamic Programming or Game Theory with Memoization. You define a state dp(i, j) as the maximum relative score Alice can get from the piles between index and . For her turn, she chooses between piles[i] - dp(i+1, j) and piles[j] - dp(i, j-1). This minimax approach is standard for two-player zero-sum games.
Piles: [3, 9, 1, 2].
For Game Theory interview pattern problems, always check if there's a symmetry or a parity argument that allows the first player to win. If not, fall back to the Minimax or Dynamic Programming approach.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Stone Game VII | Medium | Solve | |
| Stone Game III | Hard | Solve | |
| Stone Game V | Hard | Solve | |
| Stone Game II | Medium | Solve | |
| Predict the Winner | Medium | Solve |