KatInterview

Enumerate All Possible Tic-Tac-Toe Game Paths

Dynamic ProgrammingAuto-graded · 18 test cases
HardAuto-gradedDynamic Programmingrecursionmemoizationgame-theory

Last updated

Tests recursive game-tree traversal with memoization to count distinct play sequences by outcome.

Problem Description

You are given a 3×3 Tic-Tac-Toe board (possibly in progress) and need to determine how many different ways the game can be completed from that position, categorized by who wins or whether it ends in a draw.

In Tic-Tac-Toe, two players—'X' and 'O'—take turns placing their mark in an empty cell. Player 'X' always goes first from an empty board. The game ends immediately when:

  1. One player forms three marks in a row (horizontally, vertically, or diagonally)—that player wins.
  2. All nine cells are filled with no winner—the game is a draw.

Important: Two game sequences are distinct if they differ in move order, even if they produce the same final board. For example, X placing marks at positions (0,0) then (0,1) is a different sequence than (0,1) then (0,0).

Your task is to count how many distinct complete game sequences exist from the given board state, broken down by outcome.

Function Signature

def enumerate_ttt_paths(
    board: Optional[List[List[str]]] = None,
    current_player: str = "X"
) -> Dict[str, int]:

Parameters

  • board: A 3×3 grid represented as a list of three lists, where each cell contains:

    • 'X' for an X mark
    • 'O' for an O mark
    • '.' for an empty cell

    If board is None, the function starts from an empty 3×3 grid.

  • current_player: Either 'X' or 'O', indicating whose turn it is. Defaults to 'X'.

The input board is guaranteed to represent a valid, ongoing game state (not already won or full).

Return Value

A dictionary containing four keys:

  • 'X': Count of distinct game sequences where X wins
  • 'O': Count of distinct game sequences where O wins
  • 'Draw': Count of distinct game sequences ending in a draw
  • 'Total': Sum of all three counts above

Constraints

  • The board is always exactly 3×3
  • The provided board state is always valid and non-terminal
  • Your solution must compute the result algorithmically; hardcoded answers will be rejected
  • Execution must complete within 2.0 seconds for an empty board
  • Empty cells are represented by '.'

Examples

Example 1

Input: enumerate_ttt_paths()

Output: {'X': 131184, 'O': 77904, 'Draw': 46080, 'Total': 255168}

Example 2

Input: enumerate_ttt_paths([['X', 'O', 'X'], ['X', 'O', 'O'], ['.', '.', '.']], 'X')

Output: {'X': 2, 'O': 1, 'Draw': 2, 'Total': 5}

Hints

Hint 1

Model this as a game tree where each node is a board state and edges represent legal moves. Count the paths from root to each type of leaf.

Hint 2

Memoization is key: the number of ways to complete a game from a given position depends only on that position and whose turn it is, not on how you arrived there.

Hint 3

Check for wins before checking if the board is full—a move that fills the last cell and completes three-in-a-row is a win, not a draw.