FastPrepTic-Tac-Toe Move Winners

Tic-Tac-Toe Move Winners

ByteDance logoByteDance● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Implement an n × n tic-tac-toe game. Each move contains [row, column, player], where player is 1 or 2. After each move, return that player if the move completes a full row, column, main diagonal or anti-diagonal; otherwise return 0.

For this exercise, represent the class calls as one finite ordered move sequence and return one result per move. Moves alternate players starting with player 1, use distinct cells and stop as soon as a player wins. Empty sequences return an empty list. Coordinates are zero-indexed.

Function

ticTacToeWinners(n: int, moves: int[][]) → int[]

Examples

Example 1

n = 3moves = [[0,0,1],[1,0,2],[0,1,1],[1,1,2],[0,2,1]]return = [0,0,0,0,1]

The final move completes the first row for player 1.

Example 2

n = 1moves = [[0,0,1]]return = [1]

The only cell completes a line.

Example 3

n = 3moves = []return = []

No calls means no outputs.

Constraints

  • 1 <= n <= 100.
  • 0 <= moves.length <= min(n^2, 500).
  • Each move is a triple with valid coordinates and player 1 or 2.
  • All moves are legal; no move follows a winning move.

More ByteDance problems

See ByteDance hiring insights
public int[] ticTacToeWinners(int n, int[][] moves) {
    // Write your code here.
}
n3
moves[[0,0,1],[1,0,2],[0,1,1],[1,1,2],[0,2,1]]
expected[0,0,0,0,1]
Checking account…