Design Tic-Tac-Toe

MediumDesigndesign

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Two players take turns placing marks on an n x n board. A player wins by filling an entire **row**, an entire **column**, the **main diagonal** (top-left to bottom-right), or the **anti-diagonal** (top-right to bottom-left) with their marks. - TicTacToe(n) creates an empty n x n board. - move(row, col, player) places player's mark (player is 1 or 2) at the empty cell (row, col). Return player if this move wins the game, otherwise 0. Every move targets an empty cell inside the board, and no moves are made after someone has won. Aim for O(1) time per move instead of rescanning the board.

Examples

Input: ["TicTacToe","move","move","move","move","move"] [[3],[1,1,1],[0,0,2],[0,2,1],[2,2,2],[2,0,1]]
Output: [null,0,0,0,0,1]
Player 1 holds (0,2), (1,1), and (2,0), which is the full anti-diagonal.
Input: ["TicTacToe","move","move","move"] [[2],[0,0,1],[1,1,2],[0,1,1]]
Output: [null,0,0,1]
On a 2x2 board, player 1 completes row 0 with its second mark.

Constraints

  • `1 <= n <= 100`
  • `player` is `1` or `2`.
  • `0 <= row, col < n`, and every move is on an empty cell.
  • No `move` is called after a player has won; at most `n^2` calls.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More design problems