Determine Color of a Chessboard Square — Problem & Solution
Determine Color of a Chessboard Square solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Determine Color of a Chessboard Square is an easy coding problem on strings and math, asked in Amazon and Adobe interviews.
- Difficulty: Easy
- Topics: Strings, Math
- Asked at: Amazon, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given coordinates, a two-character string naming a square on a standard chessboard — a file letter a-h followed by a rank digit 1-8.
Return true if that square is white and false if it is black. On a standard board, a1 is black.
Example 1
Input: coordinates = "a1"
Output: false
Explanation: The bottom-left corner is black.
Example 2
Input: coordinates = "h3"
Output: true
Example 3
Input: coordinates = "c7"
Output: false
Constraints
coordinates.length == 2The first character is between 'a' and 'h'.The second character is between '1' and '8'.
Hints for Determine Color of a Chessboard Square
Hint 1
Convert the file letter and the rank digit into 0-based numbers.
Hint 2
Neighbouring squares always differ in colour, so colour depends on the parity of their sum.
Hint 3
Fix the parity using the known anchor:
a1(0 + 0) is black.
How to solve Determine Color of a Chessboard Square
A chessboard is a parity checkerboard: the colour of a square is decided entirely by whether file + rank is even or odd.
Approach
- Map the file letter to
0..7viac - 'a'and the rank digit to0..7viad - '1'. - Compute the parity of their sum.
- Return
true(white) when the sum is odd, sincea1— sum 0, even — is black.
Why it works
Moving one square in any direction changes exactly one coordinate by one, flipping the parity of the sum — which is precisely how the alternating colours work. Anchoring on a1 fixes which parity is which.
Complexity
- Time —
O(1) - Space —
O(1)
Pitfalls
- Subtracting
'0'instead of'1'from the rank shifts the parity and inverts every answer. - Getting the anchor backwards flips the whole board — check against a known square such as
h3(white).
Reference solution
Python
def squareIsWhite(coordinates: str) -> bool:
file = ord(coordinates[0]) - ord('a')
rank = ord(coordinates[1]) - ord('1')
return (file + rank) % 2 == 1Java
public static boolean squareIsWhite(String coordinates) {
int file = coordinates.charAt(0) - 'a';
int rank = coordinates.charAt(1) - '1';
return (file + rank) % 2 == 1;
}C++
bool squareIsWhite(string coordinates) {
int file = coordinates[0] - 'a';
int rank = coordinates[1] - '1';
return (file + rank) % 2 == 1;
}JavaScript
var squareIsWhite = function(coordinates) {
const file = coordinates.charCodeAt(0) - 97;
const rank = coordinates.charCodeAt(1) - 49;
return (file + rank) % 2 === 1;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings