← Questions

Validate a tic-tac-toe board state

0
SnowflakeCodingLast asked Dec 2025

A coding interview question asked in Snowflake's two coding screens round, last asked Dec 2025. It tests games, matrices and validation.

You get a tic-tac-toe board taken at some moment of a game. Players alternate, X always moves first, and the game stops the moment someone completes a line of three (row, column or diagonal). Decide whether the board could have come from legal play, and if so, report the state of the game: who won, whether it is a draw, or whose turn it is.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • game_status(board: list[str]) -> str where board is 3 strings of length 3 over 'X', 'O' and '.', and the result is one of "invalid", "X_wins", "O_wins", "draw", "X_turn", "O_turn".

Example

game_status(["...", "...", "..."])  ->  "X_turn"
game_status(["X..", "...", "..."])  ->  "O_turn"
game_status(["XXX", "OO.", "..."])  ->  "X_wins"
game_status(["OOO", "XX.", "X.."])  ->  "O_wins"
game_status(["XOX", "XOO", "OXX"])  ->  "draw"
game_status(["XXX", "OOO", "..."])  ->  "invalid"   # both have a line
game_status(["XXX", "OOX", "OO."])  ->  "invalid"   # X won, but O moved after
game_status(["O..", "...", "..."])  ->  "invalid"   # O cannot move first

Constraints

  • The input is always 3 strings of exactly 3 characters.
  • A full board with a winner reports the winner, not a draw.

Clarifying questions

  • Can a single move complete two lines at once (yes, and that is still one win)?
  • Should a valid finished game report the winner even if the board is full?
  • Do we only need a valid/invalid answer, or the full status?
  • Will the board size change later (an n x n board, or k in a row)?

What a strong answer covers

  • The counting invariant: count(X) == count(O) or count(X) == count(O) + 1; anything else is invalid.
  • Win checks over all 8 lines, and the coupling rules: if X has won, X made the last move, so count(X) == count(O) + 1; if O has won, the counts are equal; both winning is invalid.
  • Status after validity: winner, else draw on a full board, else whose turn from the counts.
  • Writing the checks so they generalize: board size and line length as parameters, lines generated rather than hard-coded.
  • Tests for each status and for the tricky invalid cases (both win, a win with the wrong count, O first).

Follow-ups

  • Generalize to an n x n board where k in a row wins.
  • Return the set of moves that would win immediately for the player to move.
  • Given a valid board, is the game already decided under perfect play?

Where it's asked

  • Snowflake · Two coding screens · Last asked Dec 202560 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.

What it tests

games · matrices · validation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding