Given the coordinates of two squares on a standard chessboard (e.g., "a1" and "c3"), you need to determine if they have the same color. On a chessboard, the bottom-left square "a1" is black, and colors alternate both horizontally and vertically.
Companies like Meta and Amazon use this "Easy" problem to test basic string parsing and parity logic. It's a test of whether you can translate a visual pattern into a simple mathematical formula. It evaluations your ability to map characters (a-h) and digits (1-8) into a coordinate system and use the properties of even/odd sums.
The pattern is Parity-based Math. In a grid where colors alternate, a square is one color if is even, and the other color if is odd. You just need to convert the column letter to an integer (a=0, b=1, etc.) and the row digit to an integer, then check if the parities of the sums and match.
Square 1: "a1" -> x=0, y=0. Sum = 0 (Even). Square 2: "c3" -> x=2, y=2. Sum = 4 (Even). Both sums are even, so they have the same color. Square 3: "h3" -> x=7, y=2. Sum = 9 (Odd). "a1" and "h3" have different colors.
A common error is overcomplicating the logic by trying to build the entire chessboard in memory. Another mistake is miscalculating the indices (e.g., thinking 'a' is 1 and 'b' is 2, which is fine, but being inconsistent). Some might also forget that the color depends on the sum of the coordinates, not just the individual parities.
Many grid-based "color" or "pattern" problems can be solved using the formula (row + col) % 2. This is a powerful trick for any alternating pattern in two dimensions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Base 7 | Easy | Solve | |
| Day of the Year | Easy | Solve | |
| Number of Days Between Two Dates | Easy | Solve | |
| Excel Sheet Column Title | Easy | Solve | |
| Greatest Common Divisor of Strings | Easy | Solve |