Magicsheet logo

Valid Tic-Tac-Toe State

Medium
12.5%
Updated 8/1/2025

Asked by 2 Companies

Valid Tic-Tac-Toe State

What is this problem about?

The "Valid Tic-Tac-Toe State" interview question asks you to determine if a given 3x3 Tic-Tac-Toe board could have actually occurred during a real game. You must validate the number of 'X' and 'O' marks and check if the winning conditions are consistent with the rules (e.g., both players cannot win at the same time).

Why is this asked in interviews?

Microsoft and Amazon use the "Valid Tic-Tac-Toe State" coding problem to test a candidate's ability to handle multiple logical constraints. It requires you to think through the mechanics of a game: Who starts first? How many turns can each player take? What happens when someone wins?

Algorithmic pattern used

The "Matrix interview pattern" is used here. You perform a count of the characters and a series of checks on rows, columns, and diagonals.

  1. 'X' always starts, so count(X) must be either count(O) or count(O) + 1.
  2. If 'X' wins, count(X) must be count(O) + 1.
  3. If 'O' wins, count(O) must be equal to count(X).
  4. They cannot both have winning lines.

Example explanation

Board: X X X O O _


  1. count(X) = 3, count(O) = 2. Rule 1 passes (3 = 2 + 1).
  2. Does 'X' win? Yes, top row.
  3. Rule 2: If 'X' wins, is count(X) == count(O) + 1? Yes (3 = 2 + 1).
  4. Does 'O' win? No.
  5. Result: Valid State.

Common mistakes candidates make

A common error is not checking all winning conditions (rows, columns, and both diagonals). Another mistake is allowing a state where 'O' wins but 'X' has more marks; if 'O' wins, 'X' must have stopped playing immediately, so their counts must be equal. Candidates also often miss the "both win" case, which is impossible in a legal game.

Interview preparation tip

When solving the "Valid Tic-Tac-Toe State" coding problem, write a helper function isWin(board, player) to check if a specific player has a winning line. This modular approach makes the main validation logic much easier to read and debug.

Similar Questions