Problem · Matrix

Validate a Two-Color Chessboard

Learn this problem
EasyAmazon logoAmazonFULLTIMEONSITE INTERVIEW
See Amazon hiring insights

Problem statement

You are given a nonempty rectangular integer matrix board. Every cell is one of two colors, encoded as 0 or 1.

Return true if board is a valid chessboard pattern: every pair of horizontally adjacent cells has different colors, and every pair of vertically adjacent cells has different colors. Otherwise, return false.

Function

isValidChessboard(board: int[][]) → boolean

Examples

Example 1

board = [[0,1,0],[1,0,1]]return = true

Every horizontal and vertical neighbor has the opposite color.

Example 2

board = [[0,1],[1,1]]return = false

The two cells in the bottom row have the same color, so the pattern is invalid.

Constraints

  • 1 <= board.length <= 100
  • 1 <= board[i].length <= 100
  • Every row has the same length.
  • board[i][j] is either 0 or 1.

More Amazon problems

drafts saved locally
public boolean isValidChessboard(int[][] board) {
    // Write your code here.
}
board[[0,1,0],[1,0,1]]
expectedtrue
checking account