Validate an extended tic-tac-toe state
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem
You are given a 3×3 tic-tac-toe board state as an array of 3 strings, each of length 3. Each cell is one of:
- `'X'` (player X)
- `'O'` (player O)
- `'.'` (empty)
Assume players alternate turns starting with X, and the game ends immediately when a player gets 3 in a row (row, column, or diagonal). No moves can be played after the game ends.
### Task (extended vs. basic validity)
Write a function that returns the **game status** for the given board:
- `"invalid"` if the board cannot occur from a valid sequence of moves.
- `"X_wins"` if X has already won in a valid way.
- `"O_wins"` if O has already won in a valid way.
- `"draw"` if the board is full and there is no winner.
- `"X_turn"` if the state is valid and it is X's turn to play next.
- `"O_turn"` if the state is valid and it is O's turn to play next.
### Input
- `board`: `string[3]`
### Output
- One of: `invalid`, `X_wins`, `O_wins`, `draw`, `X_turn`, `O_turn`
### Notes / Constraints
- The input is always 3 strings of length 3.
- You must enforce turn counts (X goes first, players alternate).
- You must enforce the “stop playing after win” rule.
- Consider tricky cases like both players appearing to win, or a win with an impossible move count.
Quick Answer: This question evaluates the ability to validate game states by reasoning about turn counts, win detection, and termination rules, testing competencies in game-state invariants and correctness checks within the Coding & Algorithms domain.
You are given a 3x3 tic-tac-toe board represented as an array of 3 strings, each of length 3. Each cell is one of: 'X', 'O', or '.'. Players alternate turns starting with X. The game ends immediately when a player gets 3 in a row horizontally, vertically, or diagonally, and no further moves may be played after that.
Return the game status for the given board:
- 'invalid' if the board cannot occur from a valid sequence of moves
- 'X_wins' if X has already won in a valid way
- 'O_wins' if O has already won in a valid way
- 'draw' if the board is full and there is no winner
- 'X_turn' if the state is valid and it is X's turn next
- 'O_turn' if the state is valid and it is O's turn next
Constraints
- board always contains exactly 3 strings, each of length 3
- Each character is one of 'X', 'O', or '.'
- X always moves first, and players strictly alternate turns
- The game stops immediately after the first winning line is created
Examples
Input: (['...','...','...'],)
Expected Output: 'X_turn'
Explanation: No moves have been played yet, so the state is valid and X goes first.
Input: (['X..','...','...'],)
Expected Output: 'O_turn'
Explanation: X has made the first move and there is no winner, so it is now O's turn.
Hints
- Start by counting how many X's and O's are on the board. Only two count relationships are ever possible in a valid game.
- Check all 8 possible winning lines. If X wins, X must have exactly one more move than O. If O wins, the counts must be equal.