You are given a string array representing a Tic-Tac-Toe board. Each element of the array represents a row of the board. The characters in the board will be either 'X', 'O', or ' ' (representing an empty space).
Your task is to determine if the given board position is valid according to the rules of Tic-Tac-Toe. A valid board position is one that could be reached during a legitimate game.
Here are the rules of Tic-Tac-Toe:
For example:
["O "," "," "]
is invalid because 'X' always plays first.["XOX"," X "," "]
is invalid because players must take turns.["XOX","O O","XOX"]
is valid.Write a function that takes a string array representing the Tic-Tac-Toe board and returns true
if the board position is valid, and false
otherwise.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to validating a Tic-Tac-Toe board means we check every possible condition that makes a board valid or invalid. We will exhaustively look at all the possibilities for wins and number of moves, marking the board as valid only if it satisfies all criteria.
Here's how the algorithm would work step-by-step:
def valid_tic_tac_toe_state(board):
number_of_x = 0
number_of_o = 0
for row in board:
for cell in row:
if cell == 'X':
number_of_x += 1
elif cell == 'O':
number_of_o += 1
if number_of_o > number_of_x or number_of_x - number_of_o > 1:
return False
x_wins = check_win(board, 'X')
o_wins = check_win(board, 'O')
# If both win, the game state is invalid
if x_wins and o_wins:
return False
if x_wins:
# If 'X' wins, there cannot be more 'O's than 'X's
if number_of_o > number_of_x:
return False
if o_wins:
# If 'O' wins, 'X' and 'O' must have the same number of pieces
if number_of_x != number_of_o:
return False
#If no one won, consider the count of marks
if not x_wins and not o_wins:
if number_of_x == number_of_o or number_of_x == number_of_o + 1:
return True
else:
return False
return True
def check_win(board, player):
# Check rows
for row in board:
if all(cell == player for cell in row):
return True
# Check columns
for column in range(3):
if all(board[row][column] == player for row in range(3)):
return True
# Check diagonals
if (board[0][0] == player and board[1][1] == player and board[2][2] == player):
return True
if (board[0][2] == player and board[1][1] == player and board[2][0] == player):
return True
return False
To determine if a Tic-Tac-Toe board is valid, we need to check if the number of 'X's and 'O's are within the allowed range and if there is a valid winner. The key is to efficiently count the marks and check for winning conditions without overcomplicating the logic.
Here's how the algorithm would work step-by-step:
def valid_tic_tac_toe_state(board):
number_of_x = 0
number_of_o = 0
for row in board:
for cell in row:
if cell == 'X':
number_of_x += 1
elif cell == 'O':
number_of_o += 1
# Check if the difference between Xs and Os is valid.
if not (number_of_x == number_of_o or number_of_x == number_of_o + 1):
return False
def check_win(mark):
# Check rows
for row in board:
if all(cell == mark for cell in row):
return True
# Check columns
for column in range(3):
if all(board[row][column] == mark for row in range(3)):
return True
# Check diagonals
if (board[0][0] == mark and board[1][1] == mark and board[2][2] == mark):
return True
if (board[0][2] == mark and board[1][1] == mark and board[2][0] == mark):
return True
return False
x_wins = check_win('X')
o_wins = check_win('O')
# If both X and O win, the board is invalid.
if x_wins and o_wins:
return False
# Validate winner based on X and O counts.
if x_wins:
if number_of_x != number_of_o + 1:
return False
if o_wins:
if number_of_x != number_of_o:
return False
return True
Case | How to Handle |
---|---|
Null or empty board | Return true (or false, depending on specified expected behavior for invalid input) immediately since it's not a valid game state. |
Board with invalid characters (not 'X', 'O', or ' ') | Return false, as the board contains illegal characters, violating Tic-Tac-Toe rules. |
More 'O's than 'X's | Return false, since 'X' always plays first or the same number as 'O'. |
Too many 'X's or 'O's relative to board size | Return false, as it would be impossible to make that many moves in a valid Tic-Tac-Toe game on the given board. |
Both 'X' and 'O' have won | Return false, because in a standard Tic-Tac-Toe game, only one player can win. |
'X' won, but it's 'O's turn | Return false, as the game should have ended when 'X' won. |
'O' won, but it's 'X's turn and the number of 'X's and 'O's are equal | Return false because 'X' made the last move and cannot be behind. |
Board is full, but no one has won | Return true, which is a valid tie game state. |