Two Sigma OA Interview Question: Implement Connect6

15 Views
No Comments

Implement Connect6.

Connect6 is a simple board game similar to tic-tac-toe. There are two players, black and white, who alternate turns placing their colored pieces onto a grid. The first player to connect 6 or more of their pieces together horizontally, vertically, or diagonally wins.

Black places one piece first, then each player alternates placing two pieces onto the board. The board size should be K x K where K <= 10^9.

An example game may look something like this:

You should implement a class called Connect6 with the following methods:

  • constructor: Initializes the game with a k by k board.
  • getTurn: Retrieve which player should move next.
  • placeBlack: Place a black piece onto the board at the specified coordinates.
  • placeWhite: Place a white piece onto the board at the specified coordinates.

Return values:

  • getTurn: string, "black" if it is black’s turn, otherwise "white".
  • placeBlack / placeWhite: boolean, true if the move resulted in a win, else false.

This problem asks you to design a Connect6 board game class with turn tracking and win detection. Because the board can be extremely large, the usual approach is to store only occupied cells in a hash set or map rather than a full grid. After each move, check the four directions around the placed stone to count consecutive stones of the same color and determine whether a line of 6 or more has been formed. The implementation also needs to respect the special turn order: black moves first, then players alternate placing two stones per turn.

END
 0