Problem
Submissions

Solution: Design Tic-Tac-Toe

Statement

Naive approach

The naive approach to this problem is to check, after every move, whether the current player has won the game. Either player can win the game if the following conditions are met:

  • They are able to mark an entire row.
  • They are able to mark an entire column.
  • They are able to mark all the cells of one of the two diagonals.

We mark the cell identified by the given row and column indexes with the current player’s marker. Then, we check the following conditions to determine whether the current player wins:

  1. Check the current row to see whether the rest of the cells in the row are also occupied by the current player.
  2. Check the current column to see whether the rest of the cells in the column are also occupied by the current player.
  3. In case the current cell is on one of the diagonals, check to see whether the rest of the cells in that diagonal are also occupied by the current player.

After every move, we iterated up to four times over nn cells to check for each condition, row, column, diagonal (top-left to bottom-right corner), and anti-diagonal (top-right to bottom-left corner). Therefore, the time complexity is O(n)O(n). The space complexity of this naive approach is O(n2)O(n^2), because we are using a board of size n×nn \times n.

Let’s see if we can use the mark-counting technique to reduce the time and space complexity of our solution.

Optimized approach using mark counting

The following are the three kinds of win scenarios in tic-tac-toe:

  • Player 1 wins
  • Player 2 wins
  • No player wins

A player can win by marking all the cells in a row or a column, or along the diagonal, or, along the anti-diagonal. To identify whether either of the two players wins or if it’s a tie between the two players, we can efficiently count the marks made on the tic-tac-toe board.

Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.

Problem
Submissions

Solution: Design Tic-Tac-Toe

Statement

Naive approach

The naive approach to this problem is to check, after every move, whether the current player has won the game. Either player can win the game if the following conditions are met:

  • They are able to mark an entire row.
  • They are able to mark an entire column.
  • They are able to mark all the cells of one of the two diagonals.

We mark the cell identified by the given row and column indexes with the current player’s marker. Then, we check the following conditions to determine whether the current player wins:

  1. Check the current row to see whether the rest of the cells in the row are also occupied by the current player.
  2. Check the current column to see whether the rest of the cells in the column are also occupied by the current player.
  3. In case the current cell is on one of the diagonals, check to see whether the rest of the cells in that diagonal are also occupied by the current player.

After every move, we iterated up to four times over nn cells to check for each condition, row, column, diagonal (top-left to bottom-right corner), and anti-diagonal (top-right to bottom-left corner). Therefore, the time complexity is O(n)O(n). The space complexity of this naive approach is O(n2)O(n^2), because we are using a board of size n×nn \times n.

Let’s see if we can use the mark-counting technique to reduce the time and space complexity of our solution.

Optimized approach using mark counting

The following are the three kinds of win scenarios in tic-tac-toe:

  • Player 1 wins
  • Player 2 wins
  • No player wins

A player can win by marking all the cells in a row or a column, or along the diagonal, or, along the anti-diagonal. To identify whether either of the two players wins or if it’s a tie between the two players, we can efficiently count the marks made on the tic-tac-toe board.

Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.