Determine Color of a Chessboard Square

Easy
#1656Time: O(1). The lookup operation is constant time. The initialization happens once and can be considered a pre-computation step.Space: O(1). We use a fixed-size 8x8 array, which requires 64 booleans of storage. This is constant space, but it's more space than other approaches that don't require auxiliary data structures.1 company
Patterns
Data structures
Companies

Prompt

You are given coordinates, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.

Return true if the square is white, and false if the square is black.

The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.

 

Example 1:

Input: coordinates = "a1"
Output: false
Explanation: From the chessboard above, the square with coordinates "a1" is black, so return false.

Example 2:

Input: coordinates = "h3"
Output: true
Explanation: From the chessboard above, the square with coordinates "h3" is white, so return true.

Example 3:

Input: coordinates = "c7"
Output: false

 

Constraints:

  • coordinates.length == 2
  • 'a' <= coordinates[0] <= 'h'
  • '1' <= coordinates[1] <= '8'

Approaches

3 approaches with complexity analysis and trade-offs.

This approach involves pre-defining the color of each square on the chessboard and storing it in a data structure like a 2D array. When given a coordinate, we parse it to determine the row and column index and then simply look up the pre-computed color from our data structure.

Algorithm

  1. Create an 8x8 2D boolean array isWhite.
  2. Populate the isWhite array. For each cell (i, j), set isWhite[i][j] to true if (i + j) is odd, and false otherwise.
  3. Parse the input coordinates string.
  4. Convert the file character to a column index: col = coordinates.charAt(0) - 'a'.
  5. Convert the rank character to a row index: row = coordinates.charAt(1) - '1'.
  6. Return the value at isWhite[row][col].

Walkthrough

We can represent the chessboard as an 8x8 2D boolean array, say isWhite[8][8]. We would initialize this array based on the chessboard pattern. For example, isWhite[0][0] (for "a1") would be false, isWhite[0][1] (for "b1") would be true, and so on. The input coordinates string is parsed. The first character determines the column index, and the second character determines the row index. The column index can be calculated as col = coordinates.charAt(0) - 'a', and the row index as row = coordinates.charAt(1) - '1'. The result is then retrieved directly from isWhite[row][col]. While this approach works and has a constant time complexity for the lookup, it requires significant setup and memory to store the entire board state.

class Solution {    private static final boolean[][] isWhite = new boolean[8][8];     static {        for (int i = 0; i < 8; i++) {            for (int j = 0; j < 8; j++) {                // If (i+j) is odd, it's white.                if ((i + j) % 2 != 0) {                    isWhite[i][j] = true;                } else {                    isWhite[i][j] = false;                }            }        }    }     public boolean squareIsWhite(String coordinates) {        int col = coordinates.charAt(0) - 'a';        int row = coordinates.charAt(1) - '1';        return isWhite[row][col];    }}

Complexity

Time

O(1). The lookup operation is constant time. The initialization happens once and can be considered a pre-computation step.

Space

O(1). We use a fixed-size 8x8 array, which requires 64 booleans of storage. This is constant space, but it's more space than other approaches that don't require auxiliary data structures.

Trade-offs

Pros

  • Simple lookup logic.

  • Constant time complexity for the lookup itself.

Cons

  • Requires extra memory (64 booleans) compared to other approaches.

  • Verbose to set up the lookup table.

  • Less flexible if the board size were to change.

Solutions

class Solution { public boolean squareIsWhite ( String coordinates ) { return ( coordinates . charAt ( 0 ) + coordinates . charAt ( 1 )) % 2 == 1 ; } }

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.