KatInterview

Count All Valid 4×4 Three-in-a-Row Game Sequences

Trees & GraphsModel answer
MediumModel answerTrees & Graphsbacktrackingrecursiongame-tree

Last updated

Use recursion and backtracking to enumerate every ordered move sequence on a 4×4 board where three consecutive marks (not four) wins.

The Problem

You have a blank 4×4 grid. Two players take turns placing their symbol — call them X and O — with X going first and the two strictly alternating. A player wins the moment they complete three of their own marks in a straight line (horizontal, vertical, or diagonal). On a 4×4 board a "line" means a length-3 window, so each row contains two such windows (positions 0–2 and 1–3), and so do columns and both main diagonals. The instant a win is achieved the game is over; no additional moves are played.

A game ends in one of three ways:

  • X wins — X completes a length-3 line first.
  • O wins — O completes a length-3 line first.
  • Draw — every cell is filled and neither player has a three-in-a-row.

Write a function using recursion and backtracking that counts every distinct valid game sequence. Two play-throughs that reach the same final board state but via different move orders count as separate sequences.

def count_games() -> int:
    """Return the total number of valid ordered game sequences."""
    ...

Your solution should be capable of returning the count broken down by outcome (X wins, O wins, draws) if asked. You should also be prepared to discuss:

  • Your choice of board representation and the trade-offs.
  • How you detect wins efficiently without re-scanning the whole board.
  • How backtracking keeps the board clean across recursive calls.
  • The worst-case complexity and how the win-pruning helps in practice.

Constraints

  • The grid is exactly 4×4 (16 cells).
  • The win condition is three consecutive marks; four in a row contains two overlapping wins and also counts (the game would have stopped at three).
  • X always moves first; players strictly alternate.
  • Count ordered sequences (move order matters), not unique terminal board positions.
  • A single-threaded solution is expected; you may discuss symmetry reduction or memoization as extensions.

Clarifying questions worth raising

  • Does four consecutive marks trigger a win, or only exactly three? (Four contains a three-in-a-row sub-window, so most interpretations say yes — confirm.)
  • Are we counting ordered play sequences or distinct board positions? (Changes the answer by orders of magnitude.)
  • Do we need per-outcome counts or just the grand total?
  • Should board symmetries (rotations and reflections) be collapsed into one, or treated as distinct games?
  • Is the output a single integer, a generator, or the full list of move sequences?

Hints

Hint 1

A flat 16-element array is easier to mutate and restore than a 2D list, but either works — the key is that backtracking must undo every mutation before returning, so copy-on-every-call is wasteful here.

Hint 2

You don't need to scan all 24 windows after every move. Only lines that pass through the cell just played can be newly completed, so restrict your win check to the rows, columns, and diagonals that include that cell.

Hint 3

The raw upper bound on the search tree is 16! ≈ 2×10¹³, but the 'stop on win' rule prunes the tree heavily — most branches end well before all 16 cells are filled. Count sequences rather than materializing them to stay within memory.