Determine the outcome of a 3x3 tic-tac-toe game from its list of moves

Quick Overview

Given the ordered list of moves in a 3x3 tic-tac-toe game, return whether Player A won, Player B won, the game ended in a draw, or it is still pending. Tests careful grid bookkeeping across rows, columns and both diagonals, and the edge case that separates a draw from an unfinished game.

Determine the outcome of a 3x3 tic-tac-toe game from its list of moves

Company: DocuSign

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Tic-tac-toe is a two-player game played on a 3x3 grid. The rules of the game are as follows: 1. Players take turns placing their symbols in empty squares. 2. Player A always places the symbol `"X"`, while Player B places the symbol `"O"`. 3. Symbols can only be placed in empty squares and never on squares that are already filled. 4. The game ends when one player has three of their symbols in a row, column, or diagonal. 5. If all squares are filled and no player has achieved three in a row, the game ends in a draw. 6. Once the game is over, no further moves can be played. You are given a 2D integer array `moves`, where `moves[i] = [row_i, col_i]` indicates that the i-th move is played on the grid at position `(row_i, col_i)`. Determine the outcome of the game: - Return `"A"` if Player A wins. - Return `"B"` if Player B wins. - Return `"Draw"` if the game ends without a winner and all squares are filled. - Return `"Pending"` if the game is not yet complete and further moves can still be made. You can assume that the `moves` array is valid, meaning it follows the rules of tic-tac-toe, the grid starts out empty, and Player A always takes the first turn. ### Function Signature ```python def tic_tac_toe_winner(moves: list[list[int]]) -> str: ``` ### Rules - Rows and columns are 0-indexed, and `(0, 0)` is the top-left square. - Moves at even indices (0, 2, 4, ...) belong to Player A and moves at odd indices belong to Player B. - The return value is exactly one of the strings `"A"`, `"B"`, `"Draw"` or `"Pending"`. - Because the input is valid, no move is ever listed after a player has already won. ### Constraints - `1 <= len(moves) <= 9` - `len(moves[i]) == 2` - `0 <= row_i, col_i <= 2` - The moves array does not contain duplicates and follows the rules of tic-tac-toe. ### Examples ```text Input: moves = [[1,1],[0,0],[2,0],[1,0],[0,2]] Output: "A" Final board: O . X O X . X . . ``` Player A plays (1,1), (2,0) and (0,2). Those three squares form the anti-diagonal, so A wins on the fifth move. ```text Input: moves = [[0,0],[1,1],[2,0],[1,0],[2,2],[1,2]] Output: "B" Final board: X . . O O O X . X ``` Player B plays (1,1), (1,0) and (1,2), which fill the middle row. Player A holds three squares too, but (0,0), (2,0) and (2,2) are not on a common line. ```text Input: moves = [[0,0],[1,1],[2,0],[1,0],[2,2]] Output: "Pending" Final board: X . . O O . X . X ``` Neither player has three in a line, and only five of the nine squares are filled, so the game can continue.

Overview: Given the ordered list of moves in a 3x3 tic-tac-toe game, return whether Player A won, Player B won, the game ended in a draw, or it is still pending. Tests careful grid bookkeeping across rows, columns and both diagonals, and the edge case that separates a draw from an unfinished game.

DocuSign logo
DocuSign
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Tic-tac-toe is a two-player game played on a 3x3 grid. The rules of the game are as follows:

  1. Players take turns placing their symbols in empty squares.
  2. Player A always places the symbol "X" , while Player B places the symbol "O" .
  3. Symbols can only be placed in empty squares and never on squares that are already filled.
  4. The game ends when one player has three of their symbols in a row, column, or diagonal.
  5. If all squares are filled and no player has achieved three in a row, the game ends in a draw.
  6. Once the game is over, no further moves can be played.

You are given a 2D integer array moves, where moves[i] = [row_i, col_i] indicates that the i-th move is played on the grid at position (row_i, col_i). Determine the outcome of the game:

  • Return "A" if Player A wins.
  • Return "B" if Player B wins.
  • Return "Draw" if the game ends without a winner and all squares are filled.
  • Return "Pending" if the game is not yet complete and further moves can still be made.

You can assume that the moves array is valid, meaning it follows the rules of tic-tac-toe, the grid starts out empty, and Player A always takes the first turn.

Function Signature

def tic_tac_toe_winner(moves: list[list[int]]) -> str:

Rules

  • Rows and columns are 0-indexed, and (0, 0) is the top-left square.
  • Moves at even indices (0, 2, 4, ...) belong to Player A and moves at odd indices belong to Player B.
  • The return value is exactly one of the strings "A" , "B" , "Draw" or "Pending" .
  • Because the input is valid, no move is ever listed after a player has already won.

Constraints

  • 1 <= len(moves) <= 9
  • len(moves[i]) == 2
  • 0 <= row_i, col_i <= 2
  • The moves array does not contain duplicates and follows the rules of tic-tac-toe.

Examples

Input: moves = [[1,1],[0,0],[2,0],[1,0],[0,2]]
Output: "A"

Final board:
O . X
O X .
X . .

Player A plays (1,1), (2,0) and (0,2). Those three squares form the anti-diagonal, so A wins on the fifth move.

Input: moves = [[0,0],[1,1],[2,0],[1,0],[2,2],[1,2]]
Output: "B"

Final board:
X . .
O O O
X . X

Player B plays (1,1), (1,0) and (1,2), which fill the middle row. Player A holds three squares too, but (0,0), (2,0) and (2,2) are not on a common line.

Input: moves = [[0,0],[1,1],[2,0],[1,0],[2,2]]
Output: "Pending"

Final board:
X . .
O O .
X . X

Neither player has three in a line, and only five of the nine squares are filled, so the game can continue.