KatInterview

Configurable N×N Tic-Tac-Toe in React with O(N) Win Detection

System DesignModel answer
HardModel answerSystem Designreacttypescriptaccessibility

Last updated

Build a two-player, grid-size-configurable Tic-Tac-Toe React component with efficient win detection, prop reactivity, and full keyboard/screen-reader accessibility.

Flexible Grid Tic-Tac-Toe

Build a self-contained, two-player Tic-Tac-Toe component in React that works on any square grid from 3×3 up to 20×20. The board dimensions are controlled by a single size prop.

Rules of the game

  • Player X always moves first, then players alternate.
  • A player wins by filling an entire row, column, or diagonal with their mark — all N cells in that line.
  • If every cell is filled and no one has won, the game is a draw.
  • Clicking a cell that is already taken, or clicking anything after the game ends, must have no visible effect.

What to implement

1. Board rendering Render a grid of N × N clickable cells driven by the size prop. Fall back to 3 when size is omitted, not a whole number, or outside the range [3, 20].

2. Turn and move management Track whose turn it is and prevent illegal moves (occupied cells, moves after game over).

3. Win and draw detection Determine the outcome immediately after each move, without scanning the whole board. At N = 20 the grid has 400 cells; your detection logic must run in O(N) time per move (checking only the row, column, and diagonals touched by the last move).

4. Status line Display one of the following at all times:

  • Turn: X / Turn: O — while the game is in progress
  • Winner: X / Winner: O — when someone wins
  • Draw! — when all cells are filled with no winner

5. Reset button A reset action clears the board and restarts the game without unmounting the component.

6. Prop reactivity When size changes at runtime, the game must reset immediately to match the new board dimensions.

7. Accessibility

  • Mark the grid with role="grid", rows with role="row", and cells with role="gridcell".
  • Cells must be reachable and activatable via keyboard (Tab to focus, Enter or Space to play).
  • Status changes (turn, winner, draw) must be announced automatically to screen readers using an aria-live region.
  • Finished or occupied cells should remain visible and focusable; use aria-disabled rather than the disabled attribute so they stay in the tab order.

Component signature

interface TicTacToeProps {
  size?: number; // whole number 3–20, default 3
}

export const TicTacToe: React.FC<TicTacToeProps> = ({ size = 3 }) => {
  // your implementation
};

Required data-testid attributes

AttributeElement
statusThe status text element
cell-{row}-{col}Each cell button, zero-indexed (e.g. cell-0-0, cell-2-1)
reset-buttonThe reset button

Constraints and edge cases to handle

  • size values that are undefined, NaN, non-integers, strings, < 3, or > 20 must all silently fall back to 3.
  • A win on the very last empty cell is a win, not a draw.
  • The center cell of an odd-sized board lies on both diagonals — both counters must be updated.
  • Resetting mid-game must not unmount the component.

Hints

Hint 1

Rather than re-scanning the whole board after each move, keep a running counter per row, per column, and one for each diagonal. Increment by +1 for X and -1 for O. A counter reaching +N means X filled that line; -N means O did.

Hint 2

Use a useReducer with a plain action object for every state transition (play, reset). All guard logic — occupied cell, game already over — lives inside the reducer and returns the unchanged state object for illegal moves, which prevents React from triggering a re-render.

Hint 3

Handling a size prop change cleanly: pass a key={n} to an inner Game component. When n changes, React unmounts and remounts that subtree with fresh state, avoiding the stale-frame issue you get with a useEffect-based reset.