Winner Under a Divisible-by-Three Losing Rule

Quick Overview

Winner Under a Divisible-by-Three Losing Rule. Alice and Bob alternately remove any one stone, with Alice first. A player immediately loses if the sum of all removed stone values becomes divisible by three. If stones run out without such a loss, Bob wins automatically, regardless of whose turn would be next. Return whether optimal play lets Alice win.

Winner Under a Divisible-by-Three Losing Rule

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Alice and Bob alternately remove any one stone, with Alice moving first. The player who makes the total value of all removed stones divisible by three loses immediately. If all stones are removed without such a loss, Bob wins automatically, regardless of whose turn would come next. Return whether Alice wins under optimal play. ```cpp class Solution { public: bool stoneGameIX(vector<int>& stones); }; ``` ### Examples **Example 1:** ```text stones = [1,5] Output: true Explanation: Either first choice is safe, and the remaining stone makes the total 6 on Bob's turn. ``` **Example 2:** ```text stones = [3,6] Output: false Explanation: Any first move immediately gives a sum divisible by three. ``` ### Constraints - `1 <= stones.length <= 10^5`. - `1 <= stones[i] <= 10^4`. ### Clarifying Questions Stone order is irrelevant. Exhausting the array is a special Bob win, not the usual no-move loss rule. ```hint Inspect the structure After the first safe move, which nonzero remainder can be chosen without making the sum zero modulo three? ``` ### Reasoning checkpoints - Apply both the immediate losing condition and Bob’s special win when the stones are exhausted. - Justify any winning strategy against all optimal replies and explain why Alice cannot force a win in the remaining cases. ### Optional extensions If two additional stones have values divisible by three, can the winner change?

Overview: Winner Under a Divisible-by-Three Losing Rule. Alice and Bob alternately remove any one stone, with Alice first. A player immediately loses if the sum of all removed stone values becomes divisible by three. If stones run out without such a loss, Bob wins automatically, regardless of whose turn would be next. Return whether optimal play lets Alice win.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Alice and Bob alternately remove any one stone, with Alice moving first. The player who makes the total value of all removed stones divisible by three loses immediately. If all stones are removed without such a loss, Bob wins automatically, regardless of whose turn would come next. Return whether Alice wins under optimal play.

class Solution {
public:
    bool stoneGameIX(vector<int>& stones);
};

Examples

Example 1:

stones = [1,5]
Output: true
Explanation: Either first choice is safe, and the remaining stone makes the total 6 on Bob's turn.

Example 2:

stones = [3,6]
Output: false
Explanation: Any first move immediately gives a sum divisible by three.

Constraints

  • 1 <= stones.length <= 10^5 .
  • 1 <= stones[i] <= 10^4 .

Clarifying Questions Guidance

Stone order is irrelevant. Exhausting the array is a special Bob win, not the usual no-move loss rule.

Reasoning checkpoints

  • Apply both the immediate losing condition and Bob’s special win when the stones are exhausted.
  • Justify any winning strategy against all optimal replies and explain why Alice cannot force a win in the remaining cases.

Optional extensions

If two additional stones have values divisible by three, can the winner change?