The Stone Removal Game asks us to determine the winner between two players taking turns to remove a strictly decreasing number of stones from a single pile. It is a great exercise in translating game rules into a simple simulation loop without overcomplicating the logic.
The problem
Alice and Bob are playing a game with a single pile of n stones. Alice always takes the first turn.
On the very first turn, Alice must remove exactly 10 stones. After that, the rules dictate that the current player must remove exactly one stone fewer than the number of stones removed on the previous turn. For example, the second turn requires removing 9 stones, the third turn requires 8 stones, and so on.
If a player does not have enough stones left in the pile to make their required move, they immediately lose the game. There are no choices to make. Both players must take the exact required amount if they can. We need to figure out if Alice will win given the starting number of stones n.
Let's look at an example where n = 20.
- Turn 1 (Alice): The required amount is 10. The pile has 20 stones. Alice takes 10. The pile now has 10 stones left.
- Turn 2 (Bob): The required amount drops to 9. The pile has 10 stones. Bob takes 9. The pile now has 1 stone left.
- Turn 3 (Alice): The required amount drops to 8. The pile only has 1 stone. Alice cannot make her move.
Alice loses, which means Bob wins. For n = 20, the answer is false.
The key idea
When you see a game theory problem in an interview, your first instinct might be to reach for dynamic programming or minimax algorithms. Those tools are meant for games where players make choices to optimize their chances of winning.
In this game, there are no choices. The path of the game is completely forced. Alice must take 10, Bob must take 9, Alice must take 8, and so on. Because the game is entirely deterministic, we only need to simulate the steps to see who runs out of stones first.
Furthermore, the game is extremely short. The required number of stones starts at 10 and drops by 1 each turn. The sequence of required stones is 10, 9, 8, 7, 6, 5, 4, 3, 2, 1. After 10 turns, the required amount drops to 0, meaning the game naturally ends.
Because the maximum number of turns is 10, a simple loop simulating the game will run in constant time. We do not need to look for complex mathematical formulas to pass the interview. We can just play the game in code.
Approach
We can simulate the game turn by turn using a while loop.
- Create a variable called
requiredand set it to 10. This tracks how many stones the current player needs to take. - Create a boolean variable called
alice_turnand set it to true. This keeps track of whose turn it is. - Start a loop that continues as long as
n >= required. - Inside the loop, subtract
requiredfromn. - Decrease
requiredby 1 for the next turn. - Flip the
alice_turnflag to pass the turn to the other player. - When the loop finishes, it means the current player does not have enough stones to make a move. If
alice_turnis true, Alice is the one who cannot move, so she loses. Ifalice_turnis false, Bob cannot move, so Alice wins. We return the opposite ofalice_turn.
Here is a flowchart showing how the simulation loop works:
Code
class Solution:
def canAliceWin(self, n: int) -> bool:
required = 10
alice_turn = True
while n >= required:
n -= required
required -= 1
alice_turn = not alice_turn
return not alice_turnThe Python implementation uses a simple while loop. The line alice_turn = not alice_turn is a clean way to toggle the boolean flag back and forth between true and false. Finally, returning not alice_turn correctly maps the final state to the required boolean output. If the loop ends on Alice's turn, she loses, so we return false.
Complexity
- Time complexity: O(1). The loop runs at most 10 times because the
requiredamount decreases by 1 each iteration starting from 10. Even ifnis one million, the loop still stops after 10 iterations. Therefore, the time taken is constant and does not scale withn. - Space complexity: O(1). We only use a few variables (
requiredandalice_turn) to keep track of the game state. The memory used is constant.
Edge cases and common mistakes
nis less than 10: Ifnis between 0 and 9, the conditionn >= requiredis false right from the start. The loop never executes. The function returnsnot alice_turn, which evaluates to false. This is correct because Alice cannot even make her first move.nis very large: The sum of the numbers from 1 to 10 is 55. Ifnis 55 or greater, the game will go through all 10 possible turns. The loop will finish, and the winner will be determined by who was supposed to take the 11th turn. Our code handles this naturally.
Follow-ups an interviewer may ask
1. What if Alice's first move requires k stones instead of 10, and k can be up to 10^5?
If the starting requirement k is large, the simulation loop will run k times. The time complexity becomes O(k). For k = 10^5, a loop running 100,000 times is still very fast and will easily pass in an interview setting. We would just change the initial value of required to k.
2. How would you solve the game in O(1) time if k is very large (e.g., 10^9)?
If k is a billion, an O(k) simulation loop will be too slow. We would need a mathematical approach.
The number of stones taken after m turns forms an arithmetic progression: k, k-1, k-2, down to k - m + 1. The total stones consumed after m turns is the sum of this sequence.
The sum of the first m moves is m * (2k - m + 1) / 2.
We need to find the largest integer m such that this sum is less than or equal to n. We can find m in O(1) time using the quadratic formula, or in O(log k) time using binary search on the answer. Once we find the maximum m, we check if m is odd or even. If m is odd, Alice made the last valid move, so she wins. If m is even, Bob made the last valid move, so Bob wins.
3. What if players could choose to take either the required amount or exactly 1 stone?
This completely changes the problem. By introducing a choice, the game is no longer deterministic. We can no longer just simulate a single path. We would need to explore different branches of choices to see if a player can force a win.
We would solve this using dynamic programming with memoization. We would define a state (remaining_stones, required_amount, current_player). From this state, the current player would recursively check the outcome of taking required_amount stones and the outcome of taking 1 stone. If either choice leads to a state where the other player loses, the current player takes that winning path. This is a classic application of minimax logic used in games like Nim.