FastPrepConnect Four Winner

Connect Four Winner

Airbnb logoAirbnb● MediumFULLTIMEOA
Learn

Problem statement

Simulate a standard Connect Four game on a board with 6 rows and 7 columns.

The integer array moves lists the chosen column for each move in chronological order. Player 1 moves first, and the players alternate. A disc falls into the lowest empty cell in its chosen column.

Return the player number of the first player who forms four of their discs in a contiguous horizontal, vertical, or diagonal line. Return 0 if the sequence ends without a winner.

Every supplied move is legal, and the sequence contains no move after a player has won.

Function

connectFourWinner(moves: int[]) → int

Examples

Example 1

moves = [3,2,3,2,3,2,3]return = 1

Player 1 places four discs in column 3, so the final move completes a vertical line.

Example 2

moves = [0,0,1,1,2,2,3]return = 1

Player 1 occupies the bottom cells of columns 0 through 3 and wins horizontally.

Example 3

moves = [0,1,2,3]return = 0

Neither player has four connected discs when the move sequence ends.

Constraints

  • 1 <= moves.length <= 42.
  • Every value in moves is an integer from 0 through 6.
  • Each move selects a column that is not full.
  • Player 1 moves first, then the players alternate.
  • The sequence stops at the first winning move or before any player wins.

More Airbnb problems

See Airbnb hiring insights
public int connectFourWinner(int[] moves) {
    // Write your code here.
}
moves[3,2,3,2,3,2,3]
expected1
Checking account…