Decide Whether the First Player Can Force a Win at Flip Game
Last updated: November 29, 2026
Quick Overview
Two players take turns flipping two consecutive '+' characters into '--' in a string. The player who cannot move loses. Decide whether the first player can guarantee a win.
0
0
0 solved
Two players take turns flipping two consecutive '+' characters into '--' in a string. The player who cannot move loses. Decide whether the first player can guarantee a win.
A Google onsite game-search problem. The interviewer checks a win/lose recursion (a position is winning if some move leads to a losing position for the opponent), memoization on the string, and, for strong candidates, the Sprague-Grundy reduction to independent segments.
What the Interviewer Expects
- Defines winning positions recursively
- Memoizes results keyed by the board string
- Stops at the first move that leaves the opponent losing
- Explains the exponential cost without memoization
- Mentions Sprague-Grundy for a polynomial solution
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Implement the Grundy-number solution that splits the board into runs of '+'.
- What changes in the misere version where the last mover loses?
- How would you return a winning first move?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: string of
+and-, length up to about 60. - Output: True if the player to move can force a win.
- Example:
"++++"is True: flip the middle two to get"+--+", which leaves no moves....
High-Level Design
A position is winning if there exists a move after which the opponent is in a losing position. A position with no moves is losing.
Recurse: for every index i with s[i:i+2] == "++", build `t = s[:i]...