Interview Questions/Coding/Board State Review

Board State Review

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Board State Review

An interview dashboard receives saved states from a simple 3 by 3 board game. The first player always writes X, the second player always writes 0, and players must alternate turns starting with X. Some board states may be finished, some may still be in progress, and some may be impossible because the move counts or winning lines do not match a legal play history. For each saved board, classify the state without reconstructing every possible game. If the first player has already won, print the first-player victory message. If the second player has already won, print the second-player victory message. If the board is full with no winner, print draw. If the game can still continue, print whose turn is next. If the board could not arise from legal alternating turns, print illegal. Input Format: The first line contains t, the number of board snapshots. Each snapshot contains exactly three strings of length three. A cell is X, 0, or . for empty. Output Format: For each snapshot, print one of: first, second, the first player won, the second player won, draw, or illegal.

Examples

Example 1
Input:
2
X0.
.X.
..X
X0X
0X0
0X.
Output:
illegal

Explanation: The first board has a diagonal of X marks and X has one more move than 0, so the first player has legally won. In the second board, nobody has won and one empty cell remains; X and 0 have the same number of moves, so it is the first player's turn.

Approach hint

The two players do not move the same number of times after every state.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp