Quick Overview

Count every legal Tic-Tac-Toe move sequence that completes a game from a reachable board and specified next player. Treat different move orders as distinct, stop immediately at the first win, and handle already terminal positions and draws correctly.

Count Complete Tic-Tac-Toe Game Sequences

Company: Decagon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Count Complete Tic-Tac-Toe Game Sequences Two players play on a standard `3 x 3` board. `X` moves first and the players alternate. A game ends immediately after a move creates three equal marks in a row, column, or diagonal, or after the ninth move if nobody has won. Different move orders count as different game sequences even when they produce the same final board. Implement `count_completions(board, next_player)` to return the number of distinct legal move sequences that start at the supplied position and continue until the game ends. ## Function Contract ```python def count_completions(board: tuple[str, ...], next_player: str) -> int: ... ``` - `board` contains exactly nine strings in row-major order. Each entry is `"X"`, `"O"`, or `"."`. - `next_player` is either `"X"` or `"O"`. - The input is guaranteed to be reachable by alternating legal play from the empty board, and `next_player` is consistent with the mark counts. - If `board` is already terminal, return `1`: the empty continuation is the one completed sequence from that state. - Otherwise, every legal choice of the next empty cell starts a distinct continuation. ## Constraints - The board is always `3 x 3`. - A winning position is terminal even if empty cells remain. - Your implementation must not count any moves made after the first win. - Return an integer. ## Examples ```text board = ("X", "X", ".", "O", "O", ".", ".", ".", ".") next_player = "X" ``` The result includes the continuation where `X` wins immediately in the top-right cell, plus every other legal first move and all terminal continuations below it.

Quick Answer: Count every legal Tic-Tac-Toe move sequence that completes a game from a reachable board and specified next player. Treat different move orders as distinct, stop immediately at the first win, and handle already terminal positions and draws correctly.

Implement count_completions(board, next_player) for a 3 by 3 board stored as nine row-major strings: X, O, or . (empty). X moves first and turns alternate. Count distinct legal move sequences from this reachable position until the first win or a full-board draw. If the supplied board is already terminal, return 1 for the empty continuation.

Constraints

  • board contains exactly nine cells.
  • The position and next player are reachable and consistent.
  • No move may be counted after the first win.

Examples

Input: (["X","X","X","O","O",".",".",".","."], "O")

Expected Output: 1

Explanation: An existing X win is terminal.

Input: (["O","O","O","X","X",".","X",".","."], "X")

Expected Output: 1

Explanation: An existing O win is terminal.

Hints

  1. Each empty-cell choice begins a different sequence.
  2. Memoize by board state and next player.

Loading coding console...