Question presented to candidate: "You have built the classic 3 by 3 Tic-Tac-Toe before. Now make it general: the board is N by N and you need M in a row to win, both chosen by the player. The win check has to stay fast on a big board, and I want a move history where I can jump back to any earlier position and play on from there."
Requirements:
- Controls for the board size N (3 to 8 here) and the win length M (3 to N); changing either starts a new game.
- X and O alternate; filled cells and finished games refuse moves.
- After each move, detect a win of M in a row in any of the 4 directions using only the lines through that move, and highlight the winning cells; detect a draw when the board is full.
- A move list with "jump to move k"; making a new move after jumping back discards the later moves.
- Arrow-key navigation between cells.
What a strong answer should cover:
- Why the 3 x 3
LINEStable does not scale, and the last-move check: 4 directions, walk both ways, stop after M - 1 cells, O(M) per move. - Flat-array indexing (
row * n + col) and bounds checks so a scan never wraps to the next row. - History as a list of moves plus a
steppointer, with the board derived by replaying; truncating the future on a new move. - Clamping M when N shrinks, and resetting the game on any config change.
- A roving tabindex grid: one cell in the tab order, arrows move focus,
aria-disabledinstead ofdisabledso filled cells stay reachable.
Clarifying questions expected:
- "Should a line longer than M also count as a win, as in Gomoku freestyle?"
- "Does changing N or M mid-game reset the game, or should it be blocked while a game is running?"
- "Do you want undo of single moves, or full jump-to-any-move time travel?"
Code / implementation expected: Yes. A working component plus the win-check function written and explained; the interviewer will often ask for its complexity.