KatInterview

Simulate a Nested Tic-Tac-Toe Game with Board-Routing Rules

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designsimulationgame-theorystate-machine

Last updated

Tests state management for a two-layer game board with recursive win detection and dynamic move constraints based on previous plays.

Problem Statement

You are building a simulator for a variant of tic-tac-toe played on a nested grid. The game board contains nine smaller 3×3 grids arranged in a 3×3 layout, creating 81 total cells.

Gameplay follows these rules:

  1. Two players alternate turns, placing their symbol (X or O) in empty cells.
  2. When a player marks a cell within a small grid, the position of that cell dictates which small grid the opponent must use for their next move. For instance, marking the top-right cell of any small grid forces the next player to move somewhere within the top-right small grid of the overall layout.
  3. If the required small grid is unavailable (already won or completely filled), the next player may choose any cell in any available small grid.
  4. A small grid is won when a player achieves three of their symbols in a row—horizontally, vertically, or diagonally—within that grid. Once won, the entire small grid is closed to further moves.
  5. The overall game is won by capturing three small grids in a row (horizontally, vertically, or diagonally) on the meta-level 3×3 arrangement of small grids.

Implement a function that processes a sequence of operations on this game:

  • "UltimateTicTacToe": Initialize the game state (returns None).
  • "make_move": Attempt a move given [board_idx, cell_idx] where both indices are 0-8. Returns True if the move is valid and applied, False otherwise.
  • "check_winner": Returns 'X' if X has won, 'O' if O has won, 'Draw' if all small grids are complete with no overall winner, or None if the game is still in progress.

Both board_idx and cell_idx use row-major ordering (0=top-left, 1=top-center, ..., 8=bottom-right).

A move is invalid if:

  • The game has already concluded
  • Either index is out of range [0, 8] or not an integer
  • The move is to a board other than the required one (when a specific board is mandated)
  • The target small grid has already been won
  • The target cell is already occupied

Invalid moves do not change any game state and do not advance the turn.

Constraints

  • board_idx and cell_idx are in the range [0, 8]
  • The first move may be placed in any cell of any small grid
  • X always moves first
  • Operations are processed in the order given
  • The function must handle all invalid move scenarios by returning False without mutating state

Examples

Example 1

Input: nested_tictactoe_game(['UltimateTicTacToe', 'make_move', 'make_move', 'check_winner'], [[], [0, 4], [4, 0], []])

Output: [None, True, True, None]

Example 2

Input: nested_tictactoe_game(['UltimateTicTacToe', 'make_move', 'make_move', 'make_move', 'check_winner'], [[], [0, 4], [4, 0], [0, 1], []])

Output: [None, True, True, True, None]

Hints

Hint 1

Track which player's turn it is separately from the board state, and only advance the turn when a valid move is made.

Hint 2

A single helper function can detect three-in-a-row on any 9-element list, allowing you to reuse it for both small grid victories and the overall meta-grid victory.

Hint 3

After each valid move, determine whether the destination small grid is playable—if it's already won or full, the next player gets free choice of any open grid.