Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Design Tic-Tac-Toe



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Design Tic-Tac-Toe Game

Design Tic-Tac-Toe Game

Problem Statement

Create a Tic-Tac-Toe game that is played between two players on a n by n board.

The rules are as follows:

  1. Each move is made on an empty cell and is always valid.
  2. No additional moves can be made once a player has won.
  3. A player wins if they place n of their marks consecutively in any row, column, or diagonal.

Examples

Assuming n = 3, and player A uses "X" while player B uses "O":

TicTac toeGame = new TicTac(3);

toeGame.play(0, 0, 1); -> Returns 0 (no winner yet)
|X| | |
| | | |    // Player 1 marks position (0, 0)
| | | |

toeGame.play(0, 2, 2); -> Returns 0 (no winner yet)
|X| |O|
| | | |    // Player 2 marks position (0, 2)
| | | |

toeGame.play(2, 2, 1); -> Returns 0 (no winner yet)
|X| |O|
| | | |    // Player 1 marks position (2, 2)
| | |X|

toeGame.play(1, 1, 2); -> Returns 0 (no winner yet)
|X| |O|
| |O| |    // Player 2 marks position (1, 1)
| | |X|

toeGame.play(2, 0, 1); -> Returns 0 (no winner yet)
|X| |O|
| |O| |    // Player 1 marks position (2, 0)
|X| |X|

toeGame.play(1, 0, 2); -> Returns 0 (no winner yet)
|X| |O|
|O|O| |    // Player 2 marks position (1, 0)
|X| |X|

toeGame.play(2, 1, 1); -> Returns 1 (player 1 wins)
|X| |O|
|O|O| |    // Player 1 marks position (2, 1)
|X|X|X|
    

Constraints

  • 3 ≤ n ≤ 1000
  • Player ID will be either 1 or 2.
  • Moves will always be made to empty cells.