Count Complete Tic-Tac-Toe Games
Updated
Problem statement
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[]) → intExamples
Example 1
board = ["...","...","..."]return = 255168From 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 = 73It is X's turn. Summing every legal continuation from the five empty cells gives 73.
Example 3
board = ["XXX","OO.","..."]return = 1X 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 = 2Two 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.lengthis3or4.- The board is square, and every row contains exactly
board.lengthcharacters fromX,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
10empty cells; a 3 x 3 board may have up to9. - The result fits a signed 32-bit integer. With
mempty cells, the number of terminal continuations is at mostm!, and10! = 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