The Divisor Game interview question is a strategic game played between two players, Alice and Bob, starting with a number n. Alice goes first. On each player's turn, they must choose a number x such that 0 < x < n and n % x == 0. They then replace n with n - x. A player loses if they cannot make a move. The goal is to determine if Alice will win the game, assuming both players play optimally.
This problem is a classic example of game theory interview pattern. It's asked by companies like Microsoft and Amazon because it tests a candidate's ability to recognize patterns and perform mathematical induction. While it can be solved using dynamic programming, the optimal solution is a simple parity check, which reveals whether a candidate can think critically about game states and transitions.
This problem involves Math and Brainteasers. By analyzing small values of n, you can observe a pattern:
n is even, Alice can always choose x = 1, making n - 1 odd for Bob.Bob gets an odd n, any divisor x must also be odd (since odd numbers only have odd divisors). This makes n - x even for Alice.Bob gets n = 1 and cannot move.
Thus, Alice wins if n % 2 == 0.Let's look at n = 3.
x such that 3 % x == 0 and 0 < x < 3. The only choice is x = 1.3 - 1 = 2.n = 2. Bob chooses x = 1.2 - 1 = 1.n = 1. She cannot make a move.n = 2. Alice chooses x = 1, Bob gets n = 1 and loses. Alice wins.x must be strictly less than n.For game theory problems, always start by manually tracing the first 5-10 states. Patterns (like parity or cycles) often emerge that simplify the problem significantly.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Nim Game | Easy | Solve | |
| Guess Number Higher or Lower II | Medium | Solve | |
| Stone Game IV | Hard | Solve | |
| Stone Game | Medium | Solve | |
| Vowels Game in a String | Medium | Solve |