Winner When Each Move Removes a Positive Square

Quick Overview

Winner When Each Move Removes a Positive Square. Starting with n stones,Alice and Bob alternate removing a positive square number of stones no larger than the pile,with Alice first.

Winner When Each Move Removes a Positive Square

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Starting with n stones,Alice and Bob alternate removing a positive square number of stones no larger than the pile,with Alice first. A player unable to move loses. Return whether Alice wins under optimal play. ```cpp class Solution { public: bool winnerSquareGame(int n); }; ``` ### Examples **Example 1:** ```text Input: n = 3 Output: true Explanation: Alice removes1,leaving2; Bob can only leave1,and Alice takes the last stone. ``` **Example 2:** ```text Input: n = 5 Output: false Explanation: Alice can leave4 by removing1,or leave1 by removing4. Both leave Bob an immediate winning move. ``` ### Constraints - `1 <= n <= 100000`. ### Contract considerations Return the optimal-play boolean,not one possible game transcript. ```hint Read the boundary condition Use only legal positive-square removals and account for optimal replies from both players. Include small piles and exact-square piles. ``` ### Boundary reminders Return the optimal-play boolean,not one possible game transcript.

Overview: Winner When Each Move Removes a Positive Square. Starting with n stones,Alice and Bob alternate removing a positive square number of stones no larger than the pile,with Alice first.

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

Problem

Starting with n stones,Alice and Bob alternate removing a positive square number of stones no larger than the pile,with Alice first. A player unable to move loses. Return whether Alice wins under optimal play.

class Solution {
public:
    bool winnerSquareGame(int n);
};

Examples

Example 1:

Input: n = 3
Output: true
Explanation: Alice removes1,leaving2; Bob can only leave1,and Alice takes the last stone.

Example 2:

Input: n = 5
Output: false
Explanation: Alice can leave4 by removing1,or leave1 by removing4. Both leave Bob an immediate winning move.

Constraints

  • 1 <= n <= 100000 .

Contract considerations

Return the optimal-play boolean,not one possible game transcript.

Boundary reminders

Return the optimal-play boolean,not one possible game transcript.