Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Alice and Bob take turns playing a game, with Alice starting first.
You are given a string num of even length consisting of digits and '?' characters. On each turn, a player will do the following if there is still at least one '?' in num:
i where num[i] == '?'.num[i] with any digit between '0' and '9'.The game ends when there are no more '?' characters in num.
For Bob to win, the sum of the digits in the first half of num must be equal to the sum of the digits in the second half. For Alice to win, the sums must not be equal.
num = "243801", then Bob wins because 2+4+3 = 8+0+1. If the game ended with num = "243803", then Alice wins because 2+4+3 != 8+0+3.Assuming Alice and Bob play optimally, return true if Alice will win and false if Bob will win.
Example 1:
Input: num = "5023" Output: false Explanation: There are no moves to be made. The sum of the first half is equal to the sum of the second half: 5 + 0 = 2 + 3.
Example 2:
Input: num = "25??" Output: true Explanation: Alice can replace one of the '?'s with '9' and it will be impossible for Bob to make the sums equal.
Example 3:
Input: num = "?3295???" Output: false Explanation: It can be proven that Bob will always win. One possible outcome is: - Alice replaces the first '?' with '9'. num = "93295???". - Bob replaces one of the '?' in the right half with '9'. num = "932959??". - Alice replaces one of the '?' in the right half with '2'. num = "9329592?". - Bob replaces the last '?' in the right half with '7'. num = "93295927". Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7.
Constraints:
2 <= num.length <= 105num.length is even.num consists of only digits and '?'.Problem summary: Alice and Bob take turns playing a game, with Alice starting first. You are given a string num of even length consisting of digits and '?' characters. On each turn, a player will do the following if there is still at least one '?' in num: Choose an index i where num[i] == '?'. Replace num[i] with any digit between '0' and '9'. The game ends when there are no more '?' characters in num. For Bob to win, the sum of the digits in the first half of num must be equal to the sum of the digits in the second half. For Alice to win, the sums must not be equal. For example, if the game ended with num = "243801", then Bob wins because 2+4+3 = 8+0+1. If the game ended with num = "243803", then Alice wins because 2+4+3 != 8+0+3. Assuming Alice and Bob play optimally, return true if Alice will win and false if Bob will win.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
"5023"
"25??"
"?3295???"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1927: Sum Game
class Solution {
public boolean sumGame(String num) {
int n = num.length();
int cnt1 = 0, cnt2 = 0;
int s1 = 0, s2 = 0;
for (int i = 0; i < n / 2; ++i) {
if (num.charAt(i) == '?') {
cnt1++;
} else {
s1 += num.charAt(i) - '0';
}
}
for (int i = n / 2; i < n; ++i) {
if (num.charAt(i) == '?') {
cnt2++;
} else {
s2 += num.charAt(i) - '0';
}
}
return (cnt1 + cnt2) % 2 == 1 || s1 - s2 != 9 * (cnt2 - cnt1) / 2;
}
}
// Accepted solution for LeetCode #1927: Sum Game
func sumGame(num string) bool {
n := len(num)
var cnt1, cnt2, s1, s2 int
for i := 0; i < n/2; i++ {
if num[i] == '?' {
cnt1++
} else {
s1 += int(num[i] - '0')
}
}
for i := n / 2; i < n; i++ {
if num[i] == '?' {
cnt2++
} else {
s2 += int(num[i] - '0')
}
}
return (cnt1+cnt2)%2 == 1 || s1-s2 != (cnt2-cnt1)*9/2
}
# Accepted solution for LeetCode #1927: Sum Game
class Solution:
def sumGame(self, num: str) -> bool:
n = len(num)
cnt1 = num[: n // 2].count("?")
cnt2 = num[n // 2 :].count("?")
s1 = sum(int(x) for x in num[: n // 2] if x != "?")
s2 = sum(int(x) for x in num[n // 2 :] if x != "?")
return (cnt1 + cnt2) % 2 == 1 or s1 - s2 != 9 * (cnt2 - cnt1) // 2
// Accepted solution for LeetCode #1927: Sum Game
/**
* [1927] Sum Game
*
* Alice and Bob take turns playing a game, with Alice starting first.
* You are given a string num of even length consisting of digits and '?' characters. On each turn, a player will do the following if there is still at least one '?' in num:
* <ol>
* Choose an index i where num[i] == '?'.
* Replace num[i] with any digit between '0' and '9'.
* </ol>
* The game ends when there are no more '?' characters in num.
* For Bob to win, the sum of the digits in the first half of num must be equal to the sum of the digits in the second half. For Alice to win, the sums must not be equal.
*
* For example, if the game ended with num = "243801", then Bob wins because 2+4+3 = 8+0+1. If the game ended with num = "243803", then Alice wins because 2+4+3 != 8+0+3.
*
* Assuming Alice and Bob play optimally, return true if Alice will win and false if Bob will win.
*
* Example 1:
*
* Input: num = "5023"
* Output: false
* Explanation: There are no moves to be made.
* The sum of the first half is equal to the sum of the second half: 5 + 0 = 2 + 3.
*
* Example 2:
*
* Input: num = "25??"
* Output: true
* Explanation: Alice can replace one of the '?'s with '9' and it will be impossible for Bob to make the sums equal.
*
* Example 3:
*
* Input: num = "?3295???"
* Output: false
* Explanation: It can be proven that Bob will always win. One possible outcome is:
* - Alice replaces the first '?' with '9'. num = "93295???".
* - Bob replaces one of the '?' in the right half with '9'. num = "932959??".
* - Alice replaces one of the '?' in the right half with '2'. num = "9329592?".
* - Bob replaces the last '?' in the right half with '7'. num = "93295927".
* Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7.
*
*
* Constraints:
*
* 2 <= num.length <= 10^5
* num.length is even.
* num consists of only digits and '?'.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/sum-game/
// discuss: https://leetcode.com/problems/sum-game/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn sum_game(num: String) -> bool {
false
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1927_example_1() {
let num = "5023".to_string();
let result = false;
assert_eq!(Solution::sum_game(num), result);
}
#[test]
#[ignore]
fn test_1927_example_2() {
let num = "25??".to_string();
let result = true;
assert_eq!(Solution::sum_game(num), result);
}
#[test]
#[ignore]
fn test_1927_example_3() {
let num = "?3295???".to_string();
let result = false;
assert_eq!(Solution::sum_game(num), result);
}
}
// Accepted solution for LeetCode #1927: Sum Game
function sumGame(num: string): boolean {
const n = num.length;
let [cnt1, cnt2, s1, s2] = [0, 0, 0, 0];
for (let i = 0; i < n >> 1; ++i) {
if (num[i] === '?') {
++cnt1;
} else {
s1 += num[i].charCodeAt(0) - '0'.charCodeAt(0);
}
}
for (let i = n >> 1; i < n; ++i) {
if (num[i] === '?') {
++cnt2;
} else {
s2 += num[i].charCodeAt(0) - '0'.charCodeAt(0);
}
}
return (cnt1 + cnt2) % 2 === 1 || 2 * (s1 - s2) !== 9 * (cnt2 - cnt1);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.