828. Chalkboard XOR Game
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
This is a two-player impartial game where Alice and Bob take turns removing a number from a chalkboard. The game's state is determined by the numbers on the board. The winning and losing conditions are based on the bitwise XOR sum of these numbers. A player wins if, at the start of their turn, the XOR sum is 0. A player loses if their move causes the XOR sum to become 0. Both players play optimally. We need to determine if Alice, the first player, has a winning strategy.
Solution
Chalkboard XOR Game in Python
class Solution:
def xorGame(self, nums: List[int]) -> bool:
import functools
import operator
# Calculate the XOR sum of all numbers.
# A simple loop or functools.reduce can be used.
xor_sum = 0
for num in nums:
xor_sum ^= num
# Alternative: xor_sum = functools.reduce(operator.xor, nums)
# Winning conditions for Alice (the first player):
# 1. If the initial XOR sum is 0, Alice wins immediately at the start of her turn.
# 2. If the number of elements `n` is even:
# - Alice can always make a move that doesn't result in an XOR sum of 0.
# - This is because if the current XOR sum is X != 0, not all numbers can be X
# (otherwise their XOR sum would be 0, as n is even), so she can always pick a number different from X.
# - The game continues, and Bob will be the one to erase the last element,
# making the XOR sum 0, thus losing.
# - Therefore, if n is even, Alice has a winning strategy regardless of the initial XOR sum.
# If n is odd and the initial XOR sum is not 0, Alice must make a move.
# She leaves n-1 (an even number) elements for Bob.
# Now Bob is in the winning position described in point 2.
# Bob has a winning strategy, so Alice loses.
# Thus, Alice wins if the number of elements is even, OR if the initial XOR sum is 0.
return xor_sum == 0 or len(nums) % 2 == 0
Chalkboard XOR Game in Java
class Solution {
public boolean xorGame(int[] nums) {
// Calculate the XOR sum of all numbers on the chalkboard.
int xorSum = 0;
for (int num : nums) {
xorSum ^= num;
}
// Winning conditions for Alice (the first player):
// 1. If the initial XOR sum is 0, Alice wins immediately at the start of her turn.
// 2. If the number of elements `n` is even:
// - Alice can always make a move that doesn't result in an XOR sum of 0.
// - This is because if the current XOR sum is X != 0, not all numbers can be X
// (otherwise their XOR sum would be 0, as n is even), so she can always pick a number different from X.
// - The game continues, and Bob will be the one to erase the last element,
// making the XOR sum 0, thus losing.
// - Therefore, if n is even, Alice has a winning strategy regardless of the initial XOR sum.
// If n is odd and the initial XOR sum is not 0, Alice must make a move.
// She leaves n-1 (an even number) elements for Bob.
// Now Bob is in the winning position described in point 2.
// Bob has a winning strategy, so Alice loses.
// Thus, Alice wins if the number of elements is even, OR if the initial XOR sum is 0.
return xorSum == 0 || nums.length % 2 == 0;
}
}
Chalkboard XOR Game in C++
class Solution {
public:
bool xorGame(vector<int>& nums) {
// Calculate the XOR sum of all numbers on the chalkboard.
int xor_sum = 0;
for (int num : nums) {
xor_sum ^= num;
}
// Winning conditions for Alice (the first player):
// 1. If the initial XOR sum is 0, Alice wins immediately at the start of her turn.
// 2. If the number of elements `n` is even:
// - Alice can always make a move that doesn't result in an XOR sum of 0.
// - This is because if the current XOR sum is X != 0, not all numbers can be X
// (otherwise their XOR sum would be 0, as n is even), so she can always pick a number different from X.
// - The game continues, and Bob will be the one to erase the last element,
// making the XOR sum 0, thus losing.
// - Therefore, if n is even, Alice has a winning strategy regardless of the initial XOR sum.
// If n is odd and the initial XOR sum is not 0, Alice must make a move.
// She leaves n-1 (an even number) elements for Bob.
// Now Bob is in the winning position described in point 2.
// Bob has a winning strategy, so Alice loses.
// Thus, Alice wins if the number of elements is even, OR if the initial XOR sum is 0.
return xor_sum == 0 || nums.size() % 2 == 0;
}
};
Chalkboard XOR Game in TypeScript
function xorGame(nums: number[]): boolean {
// Calculate the XOR sum of all numbers on the chalkboard.
const xorSum = nums.reduce((acc, curr) => acc ^ curr, 0);
// Winning conditions for Alice (the first player):
// 1. If the initial XOR sum is 0, Alice wins immediately at the start of her turn.
// 2. If the number of elements `n` is even:
// - Alice can always make a move that doesn't result in an XOR sum of 0.
// - This is because if the current XOR sum is X != 0, not all numbers can be X
// (otherwise their XOR sum would be 0, as n is even), so she can always pick a number different from X.
// - The game continues, and Bob will be the one to erase the last element,
// making the XOR sum 0, thus losing.
// - Therefore, if n is even, Alice has a winning strategy regardless of the initial XOR sum.
// If n is odd and the initial XOR sum is not 0, Alice must make a move.
// She leaves n-1 (an even number) elements for Bob.
// Now Bob is in the winning position described in point 2.
// Bob has a winning strategy, so Alice loses.
// Thus, Alice wins if the number of elements is even, OR if the initial XOR sum is 0.
return xorSum === 0 || nums.length % 2 === 0;
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.