Skip to main content

Determine Color of a Chessboard Square - Solution & Explanation

EasyMathString13 min readAsked at: Jpmorgan
Practice this problem

Problem Statement

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'

Approach Overview

Problem Overview: A chessboard coordinate like "a1" or "h3" is given. The first character represents the column (a–h) and the second represents the row (1–8). The task is to determine whether that square is white or black on a standard chessboard.

Approach 1: Index-Based Calculation (Time: O(1), Space: O(1))

Convert the column letter into a numeric index using ASCII arithmetic (column - 'a' + 1). Convert the row character into an integer. Once both coordinates are numeric, add them together. On a chessboard, squares with an even sum are black and squares with an odd sum are white (or vice versa depending on indexing choice, but the parity relationship stays consistent). This approach relies on simple math operations and minimal string parsing.

Approach 2: Parity of Coordinates (Time: O(1), Space: O(1))

Observe the pattern of a chessboard: colors alternate every step horizontally and vertically. Convert the column letter and row digit into integers, then check whether both have the same parity (both even or both odd). If the parity matches, the square has one color; if different, it has the other. The key insight is that color changes whenever you move one step in either direction, so parity fully determines the color. This approach expresses the logic directly with a parity check such as (col + row) % 2.

Recommended for interviews: The parity-based approach is what most interviewers expect. It shows you recognized the alternating chessboard pattern and reduced the problem to a constant-time arithmetic check. The index-based calculation demonstrates the same idea but makes the coordinate conversion step explicit. Both run in O(1) time and O(1) space.

Approach 1: Index-Based Calculation

The chessboard can be considered as an 8x8 grid where each square is identified by a combination of a letter (column 'a' to 'h') and a number (row '1' to '8').

One method to determine if the square is black or white is to map these coordinates to indices: columns can be mapped to numbers (a=1, b=2, ..., h=8), and rows are directly represented by numbers (1 to 8). The color of a square can be determined by the sum of these mapped values. If the sum is even, the square is black; if odd, it is white.

The function squareIsWhite checks if the given coordinates point to a white square on a chessboard. The column character is converted into a number (1-8), the row is already a number, and their sum is evaluated. The result is true if the sum is odd, indicating a white square.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), since the algorithm performs a constant amount of work.
Space Complexity: O(1), since no additional space is used besides a few variable allocations.

Try this approach in the editor →

Approach 2: Parity of Coordinates

Another approach is to explicitly define the parity rule. We analyze the position and discern that squares in the same diagonal alternation such as (0,0), (1,1), ..., are of the same color. Here, shifting the ASCII value shifts the problem to decide via binary even-odd pairing.

This method capitalizes on the fact that using the ASCII values of the characters directly, we check the sum's parity. If it's odd, the square is white, and otherwise it's black.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), evaluates a simple expression.
Space Complexity: O(1), utilizes constant overhead.

Try this approach in the editor →

Approach 3: Pattern Recognition

Observing the chessboard, we find that two squares (x_1, y_1) and (x_2, y_2) with the same color satisfy that both x_1 + y_1 and x_2 + y_2 are either odd or even.

Therefore, we can get the corresponding coordinates (x, y) from coordinates. If x + y is odd, the square is white, and we return true; otherwise, we return false.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Index-Based Calculation

Time Complexity: O(1), since the algorithm performs a constant amount of work.
Space Complexity: O(1), since no additional space is used besides a few variable allocations.

Parity of Coordinates

Time Complexity: O(1), evaluates a simple expression.
Space Complexity: O(1), utilizes constant overhead.

Pattern Recognition—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Index-Based CalculationO(1)O(1)Clear and explicit conversion from chess notation to numeric coordinates.
Parity of CoordinatesO(1)O(1)Preferred in interviews; directly uses parity pattern of a chessboard.

Video Solution

LeetCode-1812.Determine Color of a chess board square • Journey with AI: Unlocked • 1,526 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Determine Color of a Chessboard Square easy or hard?
Determine Color of a Chessboard Square is classified as an Easy problem on LeetCode with a high acceptance rate. The challenge is recognizing the alternating chessboard pattern and converting the coordinate string into numeric indices.
Determine Color of a Chessboard Square Python/Java solution
In both Python and Java, convert the column character to a numeric index and add it to the row digit. Then return whether the sum is odd or even. The logic is identical across languages and runs in O(1) time with O(1) extra space.
How to solve Determine Color of a Chessboard Square in O(1)?
Convert the column character to an index using ASCII math and parse the row digit as an integer. Compute the sum of the two values and check its parity. If the sum is even the square is one color, if odd it is the other. This requires only constant operations, giving O(1) time and O(1) space.
What is the best approach for Determine Color of a Chessboard Square?
The most common approach checks the parity of the coordinate indices. Convert the column letter to a number (a=1 through h=8), add it to the row number, and evaluate (row + column) % 2. The parity determines the color. This runs in O(1) time and O(1) space.
Is Determine Color of a Chessboard Square asked at Google/Amazon/Meta?
Problems like this appear in coding screens that test basic pattern recognition and coordinate reasoning. While not a common hard interview problem at Google, Amazon, or Meta, similar parity or grid-coloring questions appear in early screening rounds and coding practice sets.
What data structure is used in Determine Color of a Chessboard Square?
No complex data structure is required. The solution relies on simple string parsing and arithmetic operations. The key idea comes from math and coordinate parity rather than arrays, hash maps, or trees.
What is the time complexity of Determine Color of a Chessboard Square?
The problem is solved in constant time. Only a few character conversions and a parity check are required, so the time complexity is O(1). The algorithm also uses O(1) space since no additional data structures are needed.

Ready to solve this problem?

Practice Determine Color of a Chessboard Square with our built-in code editor and test cases.

Practice on FleetCode