FastPrepTic-Tac-Toe Game Status
Problem · Matrix

Tic-Tac-Toe Game Status

Learn this problem
MediumGoogle logoGoogleFULLTIMENEW GRADPHONE SCREEN
See Google hiring insights

Problem statement

Design the move-processing logic for a generalized Tic-Tac-Toe game.

There are k players and an n x n board. Players take turns placing their own mark on an empty cell. Player numbers are 1 through k. Each move is encoded as [player, row, col], specifying which player is acting and which cell they target; no strict turn-order enforcement is applied beyond what is encoded in the move list.

A player wins as soon as they have at least 3 consecutive marks in a straight line. The line may be horizontal, vertical, diagonal, or anti-diagonal. This winning length is always 3, even when n is larger than 3.

Given the move list, return the game status after each move:

  • "Game Over" if the move is attempted after the game has already concluded (either a winner exists or a Draw has already been declared).
  • "Player X won" if player X wins on that move.
  • "Draw" if the board becomes full and nobody has won.
  • "In Progress" otherwise.

All moves in the input are within board bounds. If a move targets an already-occupied cell before the game is over, leave the board unchanged and return "Invalid Move" for that move.

Function

getGameStatus(k: int, n: int, moves: int[][]) → String[]

Examples

Example 1

k = 2n = 3moves = [[1,0,0],[2,1,0],[1,0,1],[2,1,1],[1,0,2]]return = ["In Progress","In Progress","In Progress","In Progress","Player 1 won"]

Player 1 completes three consecutive marks across the top row.

Example 2

k = 3n = 4moves = [[1,0,0],[2,0,1],[3,3,3],[1,1,1],[2,1,0],[3,2,1],[1,2,2]]return = ["In Progress","In Progress","In Progress","In Progress","In Progress","In Progress","Player 1 won"]

Player 1 completes the diagonal segment (0,0), (1,1), (2,2).

Constraints

  • 1 <= k <= 10
  • 3 <= n <= 103
  • 1 <= moves.length <= min(n2 + 5, 105)
  • Each move is encoded as [player, row, col], where 1 <= player <= k and 0 <= row, col < n.
  • No strict alternating turn order is enforced; the player acting on each move is given explicitly in the move encoding.
  • Any move attempted after the game has concluded (a winner has been declared or a Draw has been declared) must return "Game Over".

More Google problems

drafts saved locally
public String[] getGameStatus(int k, int n, int[][] moves) {
    // write your code here
}
k2
n3
moves[[1,0,0],[2,1,0],[1,0,1],[2,1,1],[1,0,2]]
expected["In Progress", "In Progress", "In Progress", "In Progress", "Player 1 won"]
checking account