LeetCode Problem

How to Solve Chalkboard XOR Game

In the Chalkboard XOR Game, Alice and Bob take turns erasing numbers. The game ends when the XOR of all numbers becomes zero. The problem asks for the optimal strategy, where Alice starts first. The key lies in analyzing the XOR operation and predicting outcomes based on the array.

GhostInterview Help

Need help with Chalkboard XOR Game without spending extra time grinding it?

GhostInterview can read Chalkboard XOR Game from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #810Array plus MathReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Array plus Math
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

In the Chalkboard XOR Game, Alice and Bob take turns erasing numbers. The game ends when the XOR of all numbers becomes zero. The problem asks for the optimal strategy, where Alice starts first. The key lies in analyzing the XOR operation and predicting outcomes based on the array.

Problem Statement

You are given an array of integers, nums, which represent numbers written on a chalkboard. Alice and Bob take turns removing one number at a time, with Alice starting first. If removing a number causes the XOR of all remaining elements on the chalkboard to become 0, the player who made that move loses. A player who starts their turn with an XOR of 0 wins the game.

The game proceeds with each player carefully considering the XOR of the array as each number is erased. The task is to determine whether Alice has a winning strategy for a given input array, based on the sequence of moves and the properties of the XOR operation.

Examples

Example 1

Input: nums = [1,1,2]

Output: false

Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums become [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.

Example 2

Input: nums = [0,1]

Output: true

Example details omitted.

Example 3

Input: nums = [1,2,3]

Output: true

Example details omitted.

Constraints

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] < 216

Solution Approach

Identify the Initial XOR Value

Begin by calculating the XOR of the entire array. If the XOR of the full array is already 0, Alice wins immediately. This is because no matter which element Alice removes, the XOR of the remaining elements will always become 0, leading her to victory.

Simulate the Erasing Process

For each move, simulate erasing a number and calculate the new XOR of the array. Analyze whether the XOR becomes 0 after each possible move. Alice can only win if she can manipulate the game state in such a way that Bob is forced to make the final losing move.

Utilize Game Theory Analysis

Game theory suggests that if the initial XOR is non-zero, Alice can force Bob into a losing position if the total number of elements in the array is odd. If the XOR is non-zero and the number of remaining elements is even, Bob has the advantage. Alice's strategy is centered around forcing Bob into a losing configuration by maintaining the right XOR value after her moves.

Complexity Analysis

MetricValue
TimeO(N)
SpaceO(1)

The time complexity is O(N) because calculating the XOR of the entire array takes linear time, and simulating the erasing process requires a single pass over the array. The space complexity is O(1) since no additional data structures are needed apart from basic variables to track the XOR value and the current game state.

What Interviewers Usually Probe

  • Candidates should demonstrate an understanding of game theory principles and XOR operations in an array.
  • Look for the ability to think ahead and simulate potential moves based on XOR outcomes.
  • Assess the candidate's ability to optimize the solution using bit manipulation rather than brute force.

Common Pitfalls or Variants

Common pitfalls

  • Overlooking the initial XOR value and immediately assuming that Alice loses.
  • Failing to correctly simulate the sequence of moves and understanding the implications of each player’s move on the overall XOR value.
  • Misunderstanding the game theory aspect, especially with regard to whether Alice or Bob has the advantage based on the array length and the XOR value.

Follow-up variants

  • What if Alice and Bob can erase multiple elements in a turn? How does that change the strategy?
  • Consider the game with a different number of players and analyze how it affects the XOR game theory.
  • What if the XOR operation is replaced with a different binary operation, like AND or OR? How does that impact the game strategy?

How GhostInterview Helps

  • GhostInterview provides detailed walkthroughs to help you understand the XOR operations and how they interact in game theory contexts.
  • With GhostInterview, you can practice simulating various moves in the Chalkboard XOR Game to master predicting the outcome based on initial conditions.
  • GhostInterview helps you sharpen your algorithm skills by offering focused explanations and practice problems that simulate interview-style questions in bit manipulation and game theory.

Topic Pages

FAQ

What is the optimal strategy in the Chalkboard XOR Game?

The optimal strategy is based on the XOR of the entire array. If the XOR is 0, Alice wins immediately. If not, Alice must force Bob into a losing position by maintaining the right XOR value after each move.

Can Bob ever win if Alice plays optimally?

Yes, if the XOR of the array is non-zero and the number of elements is even, Bob can win by responding optimally to Alice's moves.

What does the XOR operation do in the Chalkboard XOR Game?

The XOR operation is used to determine the game's outcome. If the XOR of the remaining elements on the chalkboard becomes 0, the player who made the move loses.

How does game theory apply to the Chalkboard XOR Game?

Game theory applies by analyzing how the players' moves can be optimized based on the XOR value, ensuring that Alice forces Bob into a losing move if she plays correctly.

What happens if the array size is 1?

If the array size is 1, Alice wins immediately because the XOR of the single element is the element itself, and removing it causes the XOR to become 0.

GhostInterview Solver

Need direct help with Chalkboard XOR Game instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Chalkboard XOR Game from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.