Flip Game

Asked atMicrosoft
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

You are playing a Flip Game with your friend.

You are given a string currentState that contains only '+' and '-'. You and your friend take turns to flip two consecutive '++' into '--'. The game ends when a person can no longer make a move, and therefore the other person will be the winner.

Return all possible states of the string currentState after one valid move. You may return the answer in any order. If there is no valid move, return an empty list [].

Input: currentState = "++++++" Output: ["--++++", "+--+++", "++--++", "+++--+", "++++--"]

Input: currentState = "++--++" Output: ["--.--++", "++----"]

Input: currentState = "-++-"

  • 1 <= currentState.length <= 500
  • currentState[i] is either '+' or '-'.

cpp

class Solution {
public:
    vector<string> generatePossibleNextMoves(string s) {
        // Your code goes here
    }
};

java

class Solution {
    public List<String> generatePossibleNextMoves(String s) {
        // Your code goes here
    }
}

python

class Solution:
    def generatePossibleNextMoves(self, s: str):
        # Your code goes here

javascript

class Solution {
    generatePossibleNextMoves(s) {
        // Your code goes here
    }
}

csharp

public class Solution {
    public IList<string> GeneratePossibleNextMoves(string s) {

    }
}

go

func generatePossibleNextMoves(s string) []string {

}
Stuck? Show a way to structure it+
  1. 01Clarify whether this is the one-move or two-player version
  2. 02Enumerate every consecutive ++ pair
  3. 03For game play, recurse after a flip and negate the child result
  4. 04Memoize states and restore mutable state after each trial

Reference answer

Then expect these follow-ups

  • Can you optimize with Sprague-Grundy theory?

    Tests: follow-up reasoning

  • What is the complexity of the memoized search?

    Tests: complexity analysis

Hiring now

Software engineer roles are open right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Senior Software Engineer -.NET

    Tyler Technologies · Annapolis, Maryland

  • Software Development Engineer II

    SaaS Labs · Bengaluru, Karnataka, India

  • Advanced Software Engineer - Product Architect (Legal, Safety, and Risk)

    Metropolitan Transportation Authority · New York, NY

Or practice this question first

Next question