Community Problem

Stone Game

Difficulty: Medium

Decide whether the first player wins a take-from-either-end stone game on an even-length pile, with both players playing optimally.

Stone Game

Decide whether the first player wins a take-from-either-end stone game on an even-length pile, with both players playing optimally.

MEDIUM
Free
dynamic-programming
game-theory
arrays
lunamitchell

By @lunamitchell

May 13, 2026

·

Updated May 18, 2026

1,142 views

4

4.3 (13)

I had this on a Two Sigma onsite and the interviewer's follow-up was sneaky: "once you've coded the O(n^2) interval DP, can you tell me why the answer is always true?" The DP is the textbook walkthrough; the parity argument (color the indices like a chessboard, prove that the first player can always force the higher-sum color) is the trick that turns it into a one-line solution. The catalog covered predict-the-winner, but it skipped this even-length-piles variant where the parity shortcut applies.

Stone Game

Alice and Bob play a game with a row of piles, each containing a positive number of stones. The total number of stones across all piles is odd, and the number of piles is even. Players take turns, with Alice starting. Each turn a player picks the entire pile of stones from EITHER the very beginning OR the very end of the row. The player with the most stones at the end wins.

Assuming both players play optimally, return true if and only if Alice (the first player) wins.

Examples

Example 1:

  • Input: piles = [5, 3, 4, 5]
  • Output: true
  • Explanation: Alice picks the 5 on the right (piles[3]). Bob picks either 3 or 4; whichever, Alice picks the bigger remaining and ends with >= 11 to Bob's <= 7.

Example 2:

  • Input: piles = [3, 7, 2, 3]
  • Output: true
  • Explanation: Alice picks left (3). Bob is now stuck choosing between 3 and 2, leaving Alice the 7.

Example 3:

  • Input: piles = [1, 100, 9, 1]
  • Output: true
  • Explanation: Alice picks left (1); Bob is forced into a choice that lets Alice grab the 100.

Example 4:

  • Input: piles = [10, 1]
  • Output: true
  • Explanation: Alice picks 10. Smallest possible game.

Constraints

  • 2 <= piles.length <= 500.
  • piles.length is even.
  • 1 <= piles[i] <= 500.
  • The sum of piles[i] is odd (no ties possible).

Follow-up

Why is the answer always true under these constraints? Color the indices alternately white and black like a chessboard. Because the array length is even, the white-indexed sum and the black-indexed sum are different (the total is odd). Alice can ALWAYS take from the same color: on her first move, both ends have different colors, so she picks whichever color has the larger sum. After Alice picks, Bob faces an array whose ends have the SAME color (the one Alice didn't pick). Whichever Bob picks, Alice's next move again has both colors available at the ends, and she picks her color. By induction Alice ends up with all stones of her color and wins because that color has the strictly larger sum.

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems