cswork / libraryOpen practice workspace

Coding questions · Microsoft

Implement a Tic-Tac-Toe game class

This question evaluates a candidate's competence in designing efficient stateful data structures and algorithmic optimization for an n×n game implementation, focusing on time-space trade-offs and robust API design.

Problem

Implement a class that supports playing an n × n Tic-Tac-Toe game.

Requirements

Create a class TicTacToe(n) with:

  • move(row, col, player) -> int
    • row and col are 0-indexed coordinates.
    • player is either 1 or 2.
    • Each call places the player's mark at (row, col) (assume the move is always valid and the cell is empty).
    • Return:
      • 0 if there is no winner after this move,
      • 1 if player 1 wins after this move,
      • 2 if player 2 wins after this move.

A player wins if they fill an entire row, column, main diagonal, or anti-diagonal.

Constraints (typical interview assumptions)

  • 2 <= n <= 10^4 (so you should avoid O(n) scanning per move)
  • Many moves may be made; aim for O(1) time per move and O(n) space.

Tic-Tac-Toe Move Simulation

Simulate n x n Tic-Tac-Toe moves and return winner status after each move.

Examples

Example 1

Input: (3, [(0,0,1),(0,2,2),(1,1,1),(0,1,2),(2,2,1)])
Output: [0, 0, 0, 0, 1]

Explanation: Player 1 wins diagonal.

Example 2

Input: (2, [(0,1,2),(1,1,2)])
Output: [0, 2]

Explanation: Player 2 wins column.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.