Track Tic-Tac-Toe Winners across Valid Alternating Moves

Quick Overview

Track Tic-Tac-Toe Winners across Valid Alternating Moves. Implement TicTacToe(int n) and move(row,col,player). The two players alternate, always choose a previously empty valid cell, and stop after a winner. A player w

Track Tic-Tac-Toe Winners across Valid Alternating Moves

Company: Wise

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement TicTacToe(int n) and move(row,col,player). The two players alternate, always choose a previously empty valid cell, and stop after a winner. A player wins by filling one complete row, column or either diagonal. Return 0 for no winner after the move, otherwise the winning player ID 1 or 2. ```cpp class TicTacToe { public: TicTacToe(int n); int move(int row, int col, int player); }; ``` ### Examples ```text TicTacToe(2); move(0,0,1) -> 0; move(1,0,2) -> 0; move(0,1,1) -> 1 TicTacToe(2); move(0,0,1) -> 0; move(0,1,2) -> 0; move(1,1,1) -> 1 ``` ### Constraints - 2 <= n <= 100. - Player is 1 or 2 and row/col lie in [0,n). - Move cells are unique; at most n*n moves occur. - No moves follow a winning condition. ### Optional follow-up Can you do better than O(n^2) per move?

Overview: Track Tic-Tac-Toe Winners across Valid Alternating Moves. Implement TicTacToe(int n) and move(row,col,player). The two players alternate, always choose a previously empty valid cell, and stop after a winner. A player w

Wise logo
Wise
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

Implement TicTacToe(int n) and move(row,col,player). The two players alternate, always choose a previously empty valid cell, and stop after a winner. A player wins by filling one complete row, column or either diagonal. Return 0 for no winner after the move, otherwise the winning player ID 1 or 2.

class TicTacToe {
public:
    TicTacToe(int n);
    int move(int row, int col, int player);
};

Examples

TicTacToe(2); move(0,0,1) -> 0; move(1,0,2) -> 0; move(0,1,1) -> 1
TicTacToe(2); move(0,0,1) -> 0; move(0,1,2) -> 0; move(1,1,1) -> 1

Constraints

  • 2 <= n <= 100.
  • Player is 1 or 2 and row/col lie in [0,n).
  • Move cells are unique; at most n*n moves occur.
  • No moves follow a winning condition.

Optional follow-up

Can you do better than O(n^2) per move?