Magicsheet logo

Flip Game

Easy
25.1%
Updated 6/1/2025

Asked by 1 Company

Topics

Flip Game

1. What is this problem about?

The Flip Game interview question is a string simulation based on a simple game rule. You are given a string containing only '+' and '-' characters. Two players take turns flipping two consecutive "++" into "--". Your goal is to return a list of all possible strings that can result from a single valid move.

2. Why is this asked in interviews?

Google asks the Flip Game coding problem to assess basic string manipulation and loop control. It tests whether you can accurately identify patterns (consecutive pluses) and create new strings based on those patterns. It evaluations your proficiency in String interview patterns and your ability to handle immutable data types correctly.

3. Algorithmic pattern used

This problem follows the Linear Scan and Replacement pattern.

  1. Iterate: Traverse the string from index 0 to n2n-2.
  2. Match: Check if s[i] == '+' and s[i+1] == '+'.
  3. Transform: If a match is found, create a new string where the two '+' characters are replaced by '-'.
  4. Collect: Add the new string to the result list.

4. Example explanation

String: "++++"

  1. index 0-1: "++" found. Transform to "--++".
  2. index 1-2: "++" found. Transform to "+--+".
  3. index 2-3: "++" found. Transform to "++--". Result: ["--++", "+--+", "++--"].

5. Common mistakes candidates make

  • Off-by-one: Stopping the loop at index n1n-1, which leads to checking s[i+1] out of bounds.
  • In-place mutation: Trying to modify the original string (which is impossible in languages like Java or Python) or forgetting to "reset" the string before the next valid move is processed.
  • Overlap confusion: Thinking that after flipping "++" at index 0, you can't flip at index 1. Each valid move starts from the original string independently.

6. Interview preparation tip

Practice creating substrings and concatenating them. In most languages, the most efficient way to flip characters at i and i+1 is s.substring(0, i) + "--" + s.substring(i+2). This is a vital String interview pattern skill.

Similar Questions