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.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
You are given two positive integers x and y, denoting the number of coins with values 75 and 10 respectively.
Alice and Bob are playing a game. Each turn, starting with Alice, the player must pick up coins with a total value 115. If the player is unable to do so, they lose the game.
Return the name of the player who wins the game if both players play optimally.
Example 1:
Input: x = 2, y = 7
Output: "Alice"
Explanation:
The game ends in a single turn:
Example 2:
Input: x = 4, y = 11
Output: "Bob"
Explanation:
The game ends in 2 turns:
Constraints:
1 <= x, y <= 100Problem summary: You are given two positive integers x and y, denoting the number of coins with values 75 and 10 respectively. Alice and Bob are playing a game. Each turn, starting with Alice, the player must pick up coins with a total value 115. If the player is unable to do so, they lose the game. Return the name of the player who wins the game if both players play optimally.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
2 7
4 11
can-i-win)predict-the-winner)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3222: Find the Winning Player in Coin Game
class Solution {
public String losingPlayer(int x, int y) {
int k = Math.min(x / 2, y / 8);
x -= k * 2;
y -= k * 8;
return x > 0 && y >= 4 ? "Alice" : "Bob";
}
}
// Accepted solution for LeetCode #3222: Find the Winning Player in Coin Game
func losingPlayer(x int, y int) string {
k := min(x/2, y/8)
x -= 2 * k
y -= 8 * k
if x > 0 && y >= 4 {
return "Alice"
}
return "Bob"
}
# Accepted solution for LeetCode #3222: Find the Winning Player in Coin Game
class Solution:
def losingPlayer(self, x: int, y: int) -> str:
k = min(x // 2, y // 8)
x -= k * 2
y -= k * 8
return "Alice" if x and y >= 4 else "Bob"
// Accepted solution for LeetCode #3222: Find the Winning Player in Coin Game
/**
* [3222] Find the Winning Player in Coin Game
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn losing_player(x: i32, y: i32) -> String {
let count = x.min(y / 4);
if count % 2 == 1 {
"Alice".to_owned()
} else {
"Bob".to_owned()
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3222() {
assert_eq!("Alice".to_owned(), Solution::losing_player(2, 7));
assert_eq!("Bob".to_owned(), Solution::losing_player(4, 11));
}
}
// Accepted solution for LeetCode #3222: Find the Winning Player in Coin Game
function losingPlayer(x: number, y: number): string {
const k = Math.min((x / 2) | 0, (y / 8) | 0);
x -= k * 2;
y -= k * 8;
return x && y >= 4 ? 'Alice' : 'Bob';
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
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.