Watch 9 video solutions for Find the Winning Player in Coin Game, a easy level problem involving Math, Simulation, Game Theory. This walkthrough by Programming Live with Larry has 493 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 Overview: Two players take turns performing a fixed operation on two coin piles. Each move removes 1 coin from the first pile and 4 coins from the second pile. The player who cannot make a valid move loses. Given the number of coins in both piles, determine which player wins assuming optimal play.
Approach 1: Simulation (O(k) time, O(1) space)
The most direct way is to simulate the game. As long as the piles allow a move (x >= 1 and y >= 4), subtract 1 from the first pile and 4 from the second. Track whose turn it is and flip the turn after each move. When the loop stops, the player who was supposed to move next loses, so the previous player wins.
This method models the game exactly and is useful for understanding the mechanics. However, the number of iterations equals the number of valid moves. That value can be derived mathematically, which leads to a more efficient approach. This reasoning pattern appears frequently in simulation and simple game theory problems.
Approach 2: Greedy / Mathematical Insight (O(1) time, O(1) space)
Every move always consumes the same resources: 1 coin from pile x and 4 coins from pile y. That means the total number of moves possible in the entire game is limited by whichever pile runs out first. The exact number of valid moves is k = min(x, y // 4).
Once k is known, the winner becomes a simple parity check. Alice starts the game, so if the total number of moves is odd, Alice performs the final move and wins. If the number of moves is even, Bob performs the final move and wins. This converts the problem into a constant-time calculation using basic math reasoning.
This greedy observation removes the need for simulation entirely. Instead of iterating through moves, you directly compute the maximum possible turns and determine which player gets the last one.
Recommended for interviews: The mathematical greedy approach is the expected solution. It demonstrates that you recognized the fixed resource consumption per move and reduced the game to a parity check. Explaining the simulation first shows understanding of the rules, but deriving k = min(x, y // 4) and checking odd/even shows stronger problem-solving skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulation | O(k) | O(1) | When modeling the game step-by-step to understand turn mechanics |
| Greedy / Mathematical Insight | O(1) | O(1) | Best choice when each move consumes fixed resources and total moves can be computed directly |