FastPrepCount Complete Tic-Tac-Toe Games
Problems/Count Complete Tic-Tac-Toe Games

Count Complete Tic-Tac-Toe Games

Decagon logoDecagon● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in Decagon phone screens. The reports describe counting complete legal games on three-by-three boards, and a four-by-four version with three in a row. This exercise counts the ways to continue from legal boards and limits four-by-four inputs to at most ten empty cells. The empty four-by-four case is left out. Our version is about 85% the same as the reported questions.

Given a legal partial Tic-Tac-Toe board, count its distinct legal continuations through the end of the game. The board is either 3 x 3 or 4 x 4. Cells contain X, O, or . for empty.

X starts, players alternate, and the next player is determined by the existing mark counts. A player wins as soon as it has three consecutive marks horizontally, vertically, or along either diagonal direction. On a 4 x 4 board, any consecutive length-three segment qualifies; four in a row is not required.

Stop immediately when a player wins or the board is full. Different future move orders count as different games even if they lead to the same final board. The supplied board is guaranteed reachable from an empty board without moves after a win.

Return the number of complete legal future move sequences. An already finished board has exactly one continuation: make no further moves.

For this exercise, 4 x 4 input boards have at most 10 empty cells. This is a bounded continuation-counting variant; the empty 4 x 4 board is outside the input domain. Every legal 3 x 3 board, including the empty board, remains supported.

Function

countTicTacToeGames(board: String[]) → int

Examples

Example 1

board = ["...","...","..."]return = 255168
board
0120...1...2...

From the empty board, there are 255168 distinct legal move sequences when play stops at the first win or a full board.

Example 2

board = ["XX.","OO.","..."]return = 73
board
0120XX.1OO.2...

It is X's turn. Summing every legal continuation from the five empty cells gives 73.

Example 3

board = ["XXX","OO.","..."]return = 1
board
0120XXX1OO.2...

X has already won, so no further move is legal and the empty continuation contributes one complete game.

Example 4

board = [".X.O","OOXX","XXOO","OOXX"]return = 2
board
01230.X.O1OOXX2XXOO3OOXX

Two cells are empty and X moves next. X can choose either cell, and each choice has exactly one legal completion, for a total of 2. If X chooses (0,2), O then plays (0,0) and completes the diagonal (0,0), (1,1), (2,2).

Constraints

  • board.length is 3 or 4.
  • The board is square, and every row contains exactly board.length characters from X, O, and ..
  • The position is legally reachable under alternating turns, X first, and immediate stopping after any three-in-a-row win.
  • A 4 x 4 board has at most 10 empty cells; a 3 x 3 board may have up to 9.
  • The result fits a signed 32-bit integer. With m empty cells, the number of terminal continuations is at most m!, and 10! = 3628800.

More Decagon Phone Screen questions

  • Rolling CSAT TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Batch API Requests with Retry DelaysPHONE SCREEN · Seen Sep 2026
  • Recipe Cart with Undo and RedoPHONE SCREEN · Seen Sep 2026
  • Recipe Shopping Cart DiscountsPHONE SCREEN · Seen Aug 2026
  • Versioned Recipe Shopping CartPHONE SCREEN · Seen Jul 2026
  • Nested Recipe Cart TransactionsPHONE SCREEN · Seen Jul 2026
  • Count Hits in the Last Five MinutesPHONE SCREEN · Seen Apr 2026
  • Exclusive Function Execution TimePHONE SCREEN · Seen Mar 2026
  • Largest Rectangle in a HistogramPHONE SCREEN · Seen Dec 2024

Similar questions at other companies

  • Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
  • Generate Fixed-Size CombinationsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Scramble StringExpedia · ONSITE INTERVIEWExpedia interview questions
  • Edit DistanceAstrotalk · PHONE SCREEN
  • Permutations of Unique CharactersSalesforce · PHONE SCREENSalesforce interview questions
  • Validate a Palindrome After Limited DeletionsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Combination Sum with Reusable ValuesPinterest · PHONE SCREENPinterest interview questions
  • Enumerate Right-and-Down Matrix PathsOracle · ONSITE INTERVIEWOracle interview questions
  • Phone Keypad Letter CombinationsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Beautiful ArrangementMathWorks · ONSITE INTERVIEWMathWorks interview questions

Interview reports that mention this question

  • Decagon Experienced SWE Phone Screen: 1 Experience

Decagon interview experiences

  • Decagon New Grad SWE Interviews: 1 Experience
All Decagon interview questionsSee Decagon hiring insights
Saved
class Solution {
    public int countTicTacToeGames(String[] board) {
        // Write your code here.
    }
}
Checking account…