Decide whether the first player can force a win when drawing numbers without reuse toward a target total

Quick Overview

Two players take turns drawing integers from 1 to a maximum without reuse and add each one to a running total, and whoever first reaches the target wins. Decide whether the first player can force a win under optimal play. Tests game-tree search, bitmask memoization and edge cases such as unreachable targets.

Decide whether the first player can force a win when drawing numbers without reuse toward a target total

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

In the "100 game", two players take turns adding any integer from 1 to 10 to a running total, and the player who first makes the total reach or exceed 100 wins. Now change the game so that players cannot reuse integers. The two players draw from a shared pool of the integers 1 to `max_choosable_integer` without replacement, adding each pick to the running total, and the first player whose pick makes the total reach or exceed `desired_total` wins. For example, the players might draw from the integers 1 to 15 until the total reaches at least 100. Given `max_choosable_integer` and `desired_total`, return `True` if the first player to move can force a win, otherwise return `False`. Assume both players play optimally. ### Function Signature ```python def can_first_player_win(max_choosable_integer: int, desired_total: int) -> bool: ``` ### Rules - The running total starts at 0 and the first player moves first. Players alternate, and each pick removes that integer from the pool for both players. - If `desired_total` is 0, the target is met before any move and the answer is `True`. - If the pool runs out before the total reaches `desired_total`, nobody wins and the answer is `False`. ### Constraints - `1 <= max_choosable_integer <= 20` - `0 <= desired_total <= 300` ### Examples ```text Input: max_choosable_integer = 10, desired_total = 11 Output: False ``` Whatever integer `x` the first player picks, the second player picks `11 - x`. That value is between 1 and 10 and differs from `x` because 11 is odd, so it is still in the pool, and it brings the total to exactly 11. ```text Input: max_choosable_integer = 10, desired_total = 0 Output: True ``` The target is already met before anyone moves, so the first player wins. ```text Input: max_choosable_integer = 4, desired_total = 6 Output: True ``` The first player picks 1, leaving 5 to go with 2, 3 and 4 in the pool. No single pick covers 5. After the second player takes 2, 3 or 4, at most 3 is left to go while a larger or equal number is still in the pool, so the first player reaches 6 on the next turn.

Overview: Two players take turns drawing integers from 1 to a maximum without reuse and add each one to a running total, and whoever first reaches the target wins. Decide whether the first player can force a win under optimal play. Tests game-tree search, bitmask memoization and edge cases such as unreachable targets.

LinkedIn logo
LinkedIn
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

In the "100 game", two players take turns adding any integer from 1 to 10 to a running total, and the player who first makes the total reach or exceed 100 wins.

Now change the game so that players cannot reuse integers. The two players draw from a shared pool of the integers 1 to max_choosable_integer without replacement, adding each pick to the running total, and the first player whose pick makes the total reach or exceed desired_total wins. For example, the players might draw from the integers 1 to 15 until the total reaches at least 100.

Given max_choosable_integer and desired_total, return True if the first player to move can force a win, otherwise return False. Assume both players play optimally.

Function Signature

def can_first_player_win(max_choosable_integer: int, desired_total: int) -> bool:

Rules

  • The running total starts at 0 and the first player moves first. Players alternate, and each pick removes that integer from the pool for both players.
  • If desired_total is 0, the target is met before any move and the answer is True .
  • If the pool runs out before the total reaches desired_total , nobody wins and the answer is False .

Constraints

  • 1 <= max_choosable_integer <= 20
  • 0 <= desired_total <= 300

Examples

Input: max_choosable_integer = 10, desired_total = 11
Output: False

Whatever integer x the first player picks, the second player picks 11 - x. That value is between 1 and 10 and differs from x because 11 is odd, so it is still in the pool, and it brings the total to exactly 11.

Input: max_choosable_integer = 10, desired_total = 0
Output: True

The target is already met before anyone moves, so the first player wins.

Input: max_choosable_integer = 4, desired_total = 6
Output: True

The first player picks 1, leaving 5 to go with 2, 3 and 4 in the pool. No single pick covers 5. After the second player takes 2, 3 or 4, at most 3 is left to go while a larger or equal number is still in the pool, so the first player reaches 6 on the next turn.