Decide whether the first player wins a game of subtracting divisors

Quick Overview

Two players take turns subtracting a proper divisor from the number on a chalkboard, and the player with no legal move loses. Decide whether the first player wins with optimal play. The problem tests reasoning about winning and losing game positions and backing a pattern with a proof or a small table.

Decide whether the first player wins a game of subtracting divisors

Company: Visa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Alice and Bob take turns playing a game, with Alice starting first. Initially, there is a number `n` on the chalkboard. On each player's turn, that player makes a move consisting of: - Choosing any `x` with `0 < x < n` and `n % x == 0`. - Replacing the number `n` on the chalkboard with `n - x`. If a player cannot make a move, they lose the game. Return `True` if and only if Alice wins the game, assuming both players play optimally. ### Function Signature ```python def divisor_game(n: int) -> bool: ``` ### Rules - `x` must be strictly less than the current number, so `x = n` is never allowed. - Both players play optimally: each one picks a move that wins whenever such a move exists. - Return `True` when Alice wins and `False` when Bob wins. ### Constraints - `1 <= n <= 1000` ### Examples ```text Input: n = 2 Output: True ``` Alice chooses `x = 1`, leaving 1 on the board. Bob has no valid move and loses, so Alice wins. ```text Input: n = 3 Output: False ``` Alice must choose `x = 1` (2 does not divide 3), leaving 2. Bob then chooses `x = 1`, leaving 1. Alice has no valid move and loses. ```text Input: n = 1 Output: False ``` No `x` satisfies `0 < x < 1`, so Alice cannot move on her first turn and loses.

Overview: Two players take turns subtracting a proper divisor from the number on a chalkboard, and the player with no legal move loses. Decide whether the first player wins with optimal play. The problem tests reasoning about winning and losing game positions and backing a pattern with a proof or a small table.

Visa logo
Visa
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Alice and Bob take turns playing a game, with Alice starting first.

Initially, there is a number n on the chalkboard. On each player's turn, that player makes a move consisting of:

  • Choosing any x with 0 < x < n and n % x == 0 .
  • Replacing the number n on the chalkboard with n - x .

If a player cannot make a move, they lose the game.

Return True if and only if Alice wins the game, assuming both players play optimally.

Function Signature

def divisor_game(n: int) -> bool:

Rules

  • x must be strictly less than the current number, so x = n is never allowed.
  • Both players play optimally: each one picks a move that wins whenever such a move exists.
  • Return True when Alice wins and False when Bob wins.

Constraints

  • 1 <= n <= 1000

Examples

Input: n = 2
Output: True

Alice chooses x = 1, leaving 1 on the board. Bob has no valid move and loses, so Alice wins.

Input: n = 3
Output: False

Alice must choose x = 1 (2 does not divide 3), leaving 2. Bob then chooses x = 1, leaving 1. Alice has no valid move and loses.

Input: n = 1
Output: False

No x satisfies 0 < x < 1, so Alice cannot move on her first turn and loses.