Home
← All questions

Tic-Tac-Toe (3x3 and N x N) | Frontend Machine Coding

MediumPremiumMachine Coding

Frontend Machine Coding interview practice: Build a playable Tic-Tac-Toe supporting a standard 3x3 board and a generalized N x N board.

Problem

Build a Tic-Tac-Toe game with alternating turns and win detection. Then generalize to an N x N board with a configurable win length K.

Requirements

  • 2 players alternate (X and O); clicking an empty cell places the current mark.
  • Detect a win (row, column, or diagonal) and a draw; block further moves after game over.
  • (N x N variant) Win requires K consecutive marks in any direction; allow configurable N and K.
  • (Bonus) Reset button, highlight the winning line.

Hints / Approach

  • 3x3: check 8 fixed lines after each move.
  • N x N: after placing at (r, c), check the 4 directions (horizontal, vertical, two diagonals) by counting consecutive same marks both ways from the last move — O(K) per move instead of scanning the whole board.
  • Keep board as a 2D array (or flat array). Track currentPlayer, winner, and movesCount for draw detection.
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium