Decide whether a tic-tac-toe board can be reached in a legal game

Quick Overview

Decide whether a given 3x3 tic-tac-toe board could appear during a legal game where X moves first and play stops at the first completed line. The problem tests turning game rules into invariants on move counts and winning lines, including when a winning move must have been the last one.

Decide whether a tic-tac-toe board can be reached in a legal game

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a Tic-Tac-Toe board as a string array `board`, return `True` if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game. The board is a `3 x 3` array that consists of characters `' '`, `'X'`, and `'O'`. The `' '` character represents an empty square. Here are the rules of Tic-Tac-Toe: - Players take turns placing characters into empty squares `' '`. - The first player always places `'X'` characters, while the second player always places `'O'` characters. - `'X'` and `'O'` characters are always placed into empty squares, never filled ones. - The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal. - The game also ends if all squares are non-empty. - No more moves can be played if the game is over. ### Function Signature ```python def valid_tic_tac_toe(board: list[str]) -> bool: ``` ### Rules - `board` is a list of 3 strings, one per row from top to bottom. Each string has 3 characters, one per column from left to right. - The game starts from an empty board. The given position may be from a game still in progress or from a finished game. - Return `True` when at least one sequence of legal moves produces exactly this board, and `False` otherwise. ### Constraints - `len(board) == 3` - `len(board[i]) == 3` - `board[i][j]` is either `'X'`, `'O'`, or `' '`. ### Examples ```text Input: board = ["O "," "," "] Output: False ``` The only mark on the board is an O, but X always moves first, so this position cannot occur. ```text Input: board = ["XOX"," X "," "] Output: False ``` X has played 3 times but O only once. Since the players alternate and X moves first, X can be at most one mark ahead of O. ```text Input: board = ["XXX","OO "," "] Output: True ``` X has 3 marks and O has 2, which fits alternating turns. X completed the top row with the fifth move of the game, and the game stopped there.

Overview: Decide whether a given 3x3 tic-tac-toe board could appear during a legal game where X moves first and play stops at the first completed line. The problem tests turning game rules into invariants on move counts and winning lines, including when a winning move must have been the last one.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a Tic-Tac-Toe board as a string array board, return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.

The board is a 3 x 3 array that consists of characters ' ', 'X', and 'O'. The ' ' character represents an empty square.

Here are the rules of Tic-Tac-Toe:

  • Players take turns placing characters into empty squares ' ' .
  • The first player always places 'X' characters, while the second player always places 'O' characters.
  • 'X' and 'O' characters are always placed into empty squares, never filled ones.
  • The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.
  • The game also ends if all squares are non-empty.
  • No more moves can be played if the game is over.

Function Signature

def valid_tic_tac_toe(board: list[str]) -> bool:

Rules

  • board is a list of 3 strings, one per row from top to bottom. Each string has 3 characters, one per column from left to right.
  • The game starts from an empty board. The given position may be from a game still in progress or from a finished game.
  • Return True when at least one sequence of legal moves produces exactly this board, and False otherwise.

Constraints

  • len(board) == 3
  • len(board[i]) == 3
  • board[i][j] is either 'X' , 'O' , or ' ' .

Examples

Input: board = ["O  ","   ","   "]
Output: False

The only mark on the board is an O, but X always moves first, so this position cannot occur.

Input: board = ["XOX"," X ","   "]
Output: False

X has played 3 times but O only once. Since the players alternate and X moves first, X can be at most one mark ahead of O.

Input: board = ["XXX","OO ","   "]
Output: True

X has 3 marks and O has 2, which fits alternating turns. X completed the top row with the fifth move of the game, and the game stopped there.