FastPrepConnect Four from the Last Move

Connect Four from the Last Move

Snowflake logoSnowflakeEasyFULLTIMEPHONE SCREEN
Learn

Problem statement

A rectangular Connect Four board contains 0 for empty cells and 1 or 2 for player pieces. The piece at [row, col] is the move that was just placed by player.

Return whether that move belongs to a horizontal, vertical, or diagonal contiguous run of at least four player pieces. You only need to determine whether the supplied move completes a run; do not validate gravity or earlier game history.

Function

didPlayerConnectFour(board: int[][], row: int, col: int, player: int) → boolean

Examples

Example 1

board = [[0,0,0,0],[1,1,1,1],[0,0,0,0]]row = 1col = 3player = 1return = true

The supplied move completes a horizontal run of four.

Example 2

board = [[2,0,0,0],[0,2,0,0],[0,0,2,0],[0,0,0,2]]row = 2col = 2player = 2return = true

The move lies on a four-piece diagonal.

Example 3

board = [[1,1,1,0]]row = 0col = 2player = 1return = false

Three contiguous pieces are not enough.

Constraints

  • 1 <= board.length, board[i].length <= 200.
  • All rows have equal length and every cell is 0, 1, or 2.
  • row and col identify a valid cell and player is 1 or 2.

More Snowflake problems

See Snowflake hiring insights
public boolean didPlayerConnectFour(int[][] board, int row, int col, int player) {
    // Write your solution here.
}
board[[0,0,0,0],[1,1,1,1],[0,0,0,0]]
row1
col3
player1
expectedtrue
Checking account…