Stone Game solution explained
In Stone Game the first player always wins, so the function can simply return true. With an even number of piles, the first player can take every pile at an even position or every pile at an odd position, whichever group is larger, and an odd total means the two groups are never equal. Interviewers still expect the general method: an interval DP where lead[i][j] is the best margin the player to move can get on piles i to j, equal to the larger of piles[i] - lead[i + 1][j] and piles[j] - lead[i][j - 1]. That runs in O(n^2) time.
By Evin JohnPublished 7 min read
The problem in plain words
A row holds an even number of piles of stones, each pile a positive number, and the grand total is odd, so no tie is possible. Two players alternate turns. On each turn the player to move removes either the leftmost or the rightmost pile and keeps its stones. When the row is empty, whoever holds more stones wins. Both players choose perfectly. Return true if the player who moves first wins.
| Piles | Length, total | First player's best margin | Result |
|---|---|---|---|
| 8, 15, 3, 7 | even, 33 | +11 | First player wins |
| 6, 7, 8, 7 | even, 28 | 0 | Tie: the shortcut's conditions do not hold |
| 3, 5, 1 | odd, 9 | -1 | First player loses |
| 1, 100, 2 | odd, 103 | -97 | First player loses: the 100 goes to the second player |
What the interviewer is checking
This problem has two answers, and good interviewers want both. One is the observation that makes it a one-liner, which shows you look for structure before writing code. The other is the minimax DP, which shows you can handle the general game when the observation does not apply. A candidate who only writes 'return true' and cannot explain why sounds like they memorised it. A candidate who only writes the DP has missed a chance to impress. Lead with the DP framing or the proof, but be ready to deliver both.
Why the first player always wins
Number the piles from 0. Color the even positions red and the odd positions blue. Because the count is even, the two ends of the row always have different colors at the start of the first player's turn: one end is red and the other blue. So the first player can take a pile of whichever color they choose. After that, the second player faces a row with both ends of the other color, so whatever they take is of that color, and it exposes a pile of the first player's color again. By repeating this, the first player collects every pile of one color. The total is odd, so red and blue totals differ, and the first player simply picks the heavier color. In the first example row, the blue piles 15 and 7 total 22 against red's 11.
The proof shows the first player can always win; it does not show that taking one color is the best possible play. Under perfect play the true margin can be larger than the color difference, and the DP below computes it exactly.
Brute force: plain minimax
Score the game as a margin: the stones of the player to move minus the stones of the other player from here on. On a range of piles from lo to hi, the mover either takes the left pile and then faces the opponent's best margin on the rest, or takes the right pile likewise. So the margin is the larger of piles[lo] minus margin(lo + 1, hi) and piles[hi] minus margin(lo, hi - 1). Written without a cache this branches twice per move and makes 2^(n + 1) - 1 calls: 131,071 for 16 piles.
@@stone-game/slow.py@@The pattern: interval DP on the score difference
Every call is identified by its range (lo, hi), and there are only about n^2 / 2 ranges. Solve them from short to long. Ranges of one pile give that pile as the margin. For each longer range, apply the same max formula using the two shorter ranges inside it. The answer is the margin for the whole row, and the first player wins exactly when it is positive. Storing the difference rather than two separate scores is what keeps the state to two numbers, lo and hi.
@@stone-game/fast.py@@The one-dimensional array works because, when ranges of a given size are processed left to right, lead[lo + 1] still holds the margin for (lo + 1, hi) from the previous size and lead[lo] holds the margin for (lo, hi - 1). Writing the new value into lead[lo] overwrites only what this size no longer needs. If that feels fragile in an interview, use a full n by n table; the time is the same.
@@stone-game/fast.js@@Tracing the first row
Piles 8, 15, 3, 7. Single piles give 8, 15, 3, 7. Pairs: (8, 15) gives 15 - 8 = 7, (15, 3) gives 12, (3, 7) gives 4. Triples: (8, 15, 3) is max(8 - 12, 3 - 7) = -4, so whoever moves there is behind; (15, 3, 7) is max(15 - 4, 7 - 12) = 11. The whole row is max(8 - 11, 7 - (-4)) = 11. The first player takes the 7 first, which leaves the opponent facing the losing triple 8, 15, 3, and wins by 11.
Complexity
| Approach | Time | Extra memory | Valid for |
|---|---|---|---|
| Return true | O(1) | O(1) | Even count and odd total only |
| Plain minimax | O(2^n) | O(n) stack | Any row |
| Memoized minimax on (lo, hi) | O(n^2) | O(n^2) | Any row |
| Bottom-up interval DP, one array | O(n^2) | O(n) | Any row |
Edge cases
- Two piles: the first player takes the larger one; with an odd total they cannot be equal.
- An odd number of piles: the parity argument breaks, and rows such as 3, 5, 1 make the first player lose.
- An even total: a tie is possible, as in 6, 7, 8, 7, so 'wins' must mean strictly more stones.
- An empty row, if the general function allows it: nobody has stones, so the first player does not win.
Mistakes to avoid
- Answering 'return true' without the coloring argument, then being unable to adapt when the interviewer changes the rules.
- A greedy rule such as always taking the bigger end. In 2, 9, 1, 1 it takes the 2, hands the 9 to the opponent, and loses by 7; taking the right-hand 1 first wins by 7.
- Storing two scores per state and getting confused about whose turn it is. The difference formulation removes the turn from the state.
- Filling the table in the wrong order, reading a longer range before it has been computed.
- Applying the shortcut to variants with odd lengths, more than two choices per turn, or a different scoring rule.
How to explain it out loud
Fictional example
- Candidate
- There's a trick here. With an even count, the two ends always have different index parity on my turn, so I can take every even-indexed pile or every odd-indexed one. The total is odd, so one group is bigger. I always win.
- Interviewer
- And if the number of piles were odd?
- Candidate
- Then the trick fails, so I'd solve the real game. On a range, my margin is the pile I take minus the opponent's best margin on what's left. Interval DP from short ranges to long, n squared time.
- Candidate
- For this exact problem it returns a positive margin every time, which the parity argument predicts.
Related problems
Last Stone Weight II also deals with piles of stones, but there one player chooses the order and the solution is subset sum. Number of Dice Rolls With Target Sum is a counting DP with a similar 'smaller subproblems first' build order. Ones and Zeroes is a table-filling problem worth comparing for how the loop order is chosen. Predict the Winner is the general version of this game with any number of piles, and it needs the DP.
Rehearsing this one with Natively
Stone Game is a test of explanation as much as code, since the one-line answer needs a proof. In practice sessions on Mac or Windows, Natively can read the problem from a screenshot, challenge you to justify returning true, and then change the rules to an odd number of piles so you have to write the DP. The transcript lets you check afterwards whether your coloring argument was clear the first time.
Questions people ask
Why does the first player always win Stone Game?
With an even number of piles, the first player can take every even-indexed pile or every odd-indexed pile, because the two ends always have different parity on their turn. The total is odd, so one group is larger, and they pick that one.
How do you solve Stone Game with dynamic programming?
Let lead[i][j] be the best margin for the player to move on piles i to j. It equals the larger of piles[i] - lead[i + 1][j] and piles[j] - lead[i][j - 1]. Fill short ranges first; the first player wins if lead[0][n - 1] is positive.
What is the time complexity of Stone Game?
Returning true is O(1). The interval DP for the general game is O(n^2) time and O(n) memory with one rolling array.
When does the return-true shortcut fail?
When the number of piles is odd, such as 3, 5, 1, or when the total is even and a tie is possible, such as 6, 7, 8, 7. Those cases need the DP.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
