Valid Tic-Tac-Toe State

MediumMatrixsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a 3 x 3 tic-tac-toe board as a list of three strings board, where each character is 'X', 'O', or ' ' (an empty square). A game starts on an empty board. Players alternate placing their mark on an empty square, with 'X' always moving first. The game stops immediately once one player has three marks in a row, column, or diagonal, or once the board is full; no more moves are made after that. Return true if board could appear at some point during a legal game, and false otherwise.

Examples

Input: board = ["O "," "," "]
Output: false
`X` always moves first, so `O` cannot have a mark while `X` has none.
Input: board = ["XOX"," X "," "]
Output: false
`X` has three marks but `O` has one; the players must alternate.
Input: board = ["XOX","O O","XOX"]
Output: true
Four marks each, no completed line, `O` just moved.

Constraints

  • `board.length == 3`
  • `board[i].length == 3`
  • `board[i][j]` is `'X'`, `'O'`, or `' '`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems