Check if Two Chessboard Squares Have the Same Color - Problem
Chess Square Color Checker

Given two chessboard coordinates like "a1" and "h3", determine if they represent squares of the same color on a standard 8ร—8 chessboard.

๐Ÿ Goal: Return true if both squares have the same color (both black or both white), false otherwise.

๐Ÿ“ Input: Two strings representing valid chessboard coordinates
โ€ข First character: column letter ('a' to 'h')
โ€ข Second character: row number ('1' to '8')

๐Ÿ“ค Output: Boolean indicating if squares have the same color

Fun fact: On a chessboard, square 'a1' is always a dark square, and colors alternate in a checkerboard pattern!

Input & Output

example_1.py โ€” Basic Different Colors
$ Input: coordinate1 = "a1", coordinate2 = "c3"
โ€บ Output: true
๐Ÿ’ก Note: Square 'a1': column a(1) + row 1 = 2 (even) โ†’ white. Square 'c3': column c(3) + row 3 = 6 (even) โ†’ white. Both are white, so return true.
example_2.py โ€” Different Colors
$ Input: coordinate1 = "a1", coordinate2 = "h3"
โ€บ Output: false
๐Ÿ’ก Note: Square 'a1': column a(1) + row 1 = 2 (even) โ†’ white. Square 'h3': column h(8) + row 3 = 11 (odd) โ†’ black. Different colors, so return false.
example_3.py โ€” Same Square
$ Input: coordinate1 = "a1", coordinate2 = "a1"
โ€บ Output: true
๐Ÿ’ก Note: Both coordinates refer to the same square 'a1', so they obviously have the same color. Return true.

Constraints

  • coordinate1.length == coordinate2.length == 2
  • First character is a lowercase letter from 'a' to 'h'
  • Second character is a digit from '1' to '8'
  • Coordinates always represent valid chessboard squares

Visualization

Tap to expand
Chessboard Color Pattern8ร—8 Chessboard (showing sums)23456789345687abPattern Ruleโ€ข Column letter โ†’ number (a=1, b=2, ... h=8)โ€ข Add column + row numbersโ€ข Even sum = White squareโ€ข Odd sum = Black squareExample: 'a1' โ†’ 1+1=2 (even) โ†’ WhiteExample: 'b1' โ†’ 2+1=3 (odd) โ†’ Black
Understanding the Visualization
1
Coordinate System
Columns a-h map to 1-8, rows are already 1-8
2
Sum Calculation
Add column number and row number for each square
3
Color Rule
Even sum = white square, odd sum = black square
4
Comparison
Same parity means same color
Key Takeaway
๐ŸŽฏ Key Insight: Chessboard colors follow a simple mathematical pattern - the sum of coordinate numbers determines the color, making this an O(1) problem!
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
32.0K Views
Medium Frequency
~8 min Avg. Time
850 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