Determine Color of a Chessboard Square - Problem

Imagine you're developing a chess game and need to determine the color of any square on the chessboard. You are given a coordinate string that represents a square on a standard 8ร—8 chessboard.

A chessboard follows a specific pattern where squares alternate between white and black colors. The bottom-left square (a1) is always a dark square, and colors alternate from there.

Your task: Given coordinates like "a1", "c7", or "h8", return true if the square is white, and false if it's black.

The coordinate format is always: letter first (a-h representing columns), then number second (1-8 representing rows).

Input & Output

example_1.py โ€” Basic White Square
$ Input: coordinates = "a1"
โ€บ Output: false
๐Ÿ’ก Note: Square a1 is black. Using the mathematical approach: a=1, row=1, sum=1+1=2 (even), so it's black.
example_2.py โ€” Basic Black Square
$ Input: coordinates = "h3"
โ€บ Output: false
๐Ÿ’ก Note: Square h3 is black. Using the formula: h=8, row=3, sum=8+3=11 (odd), so it should be white... Wait, let me recalculate based on the actual chessboard pattern.
example_3.py โ€” Corner Case
$ Input: coordinates = "c7"
โ€บ Output: true
๐Ÿ’ก Note: Square c7 is white. Using our formula: c=3, row=7, sum=3+7=10 (even). Since a1 is black and we want even sums to be white, we need to adjust our logic.

Constraints

  • coordinates.length == 2
  • coordinates[0] is a letter from 'a' to 'h'
  • coordinates[1] is a digit from '1' to '8'
  • The coordinate will always represent a valid chessboard square

Visualization

Tap to expand
Chessboard Color Pattern12345678abcdefgh23453456Magic Formula:col_num + row_numIf ODD โ†’ WHITEIf EVEN โ†’ BLACKWorks for ANY square!
Understanding the Visualization
1
Observe the Pattern
Notice that colors alternate in both directions
2
Find the Formula
Realize that coordinate sum parity determines color
3
Apply Mathematics
Use modular arithmetic to get instant results
Key Takeaway
๐ŸŽฏ Key Insight: Chessboard colors follow a beautiful mathematical pattern - the sum of coordinate numbers determines the color through simple parity checking!
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 5
28.5K Views
Low Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen