Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
You are given two strings, coordinate1 and coordinate2, representing the coordinates of a square on an 8 x 8 chessboard.
Below is the chessboard for reference.
Return true if these two squares have the same color and false otherwise.
The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).
Example 1:
Input: coordinate1 = "a1", coordinate2 = "c3"
Output: true
Explanation:
Both squares are black.
Example 2:
Input: coordinate1 = "a1", coordinate2 = "h3"
Output: false
Explanation:
Square "a1" is black and "h3" is white.
Constraints:
coordinate1.length == coordinate2.length == 2'a' <= coordinate1[0], coordinate2[0] <= 'h''1' <= coordinate1[1], coordinate2[1] <= '8'Problem summary: You are given two strings, coordinate1 and coordinate2, representing the coordinates of a square on an 8 x 8 chessboard. Below is the chessboard for reference. Return true if these two squares have the same color and false otherwise. The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"a1" "c3"
"a1" "h3"
determine-color-of-a-chessboard-square)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3274: Check if Two Chessboard Squares Have the Same Color
class Solution {
public boolean checkTwoChessboards(String coordinate1, String coordinate2) {
int x = coordinate1.charAt(0) - coordinate2.charAt(0);
int y = coordinate1.charAt(1) - coordinate2.charAt(1);
return (x + y) % 2 == 0;
}
}
// Accepted solution for LeetCode #3274: Check if Two Chessboard Squares Have the Same Color
func checkTwoChessboards(coordinate1 string, coordinate2 string) bool {
x := coordinate1[0] - coordinate2[0]
y := coordinate1[1] - coordinate2[1]
return (x+y)%2 == 0
}
# Accepted solution for LeetCode #3274: Check if Two Chessboard Squares Have the Same Color
class Solution:
def checkTwoChessboards(self, coordinate1: str, coordinate2: str) -> bool:
x = ord(coordinate1[0]) - ord(coordinate2[0])
y = int(coordinate1[1]) - int(coordinate2[1])
return (x + y) % 2 == 0
// Accepted solution for LeetCode #3274: Check if Two Chessboard Squares Have the Same Color
/**
* [3274] Check if Two Chessboard Squares Have the Same Color
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn check_two_chessboards(coordinate1: String, coordinate2: String) -> bool {
let mut c1 = coordinate1.chars();
let mut c2 = coordinate2.chars();
Self::get_color(c1.next().unwrap(), c1.next().unwrap())
== Self::get_color(c2.next().unwrap(), c2.next().unwrap())
}
fn get_color(x: char, y: char) -> bool {
let y = y.to_digit(10).unwrap();
match x {
'a' | 'c' | 'e' | 'g' => y % 2 == 1,
'b' | 'd' | 'f' | 'h' => y % 2 == 0,
_ => unreachable!(),
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3274() {
assert!(Solution::check_two_chessboards(
"a1".to_owned(),
"c3".to_owned()
));
assert!(!Solution::check_two_chessboards(
"a1".to_owned(),
"h3".to_owned()
));
}
}
// Accepted solution for LeetCode #3274: Check if Two Chessboard Squares Have the Same Color
function checkTwoChessboards(coordinate1: string, coordinate2: string): boolean {
const x = coordinate1.charCodeAt(0) - coordinate2.charCodeAt(0);
const y = coordinate1.charCodeAt(1) - coordinate2.charCodeAt(1);
return (x + y) % 2 === 0;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.