You are given two strings, coordinate1
and coordinate2
, representing the coordinates of a square on an 8 x 8
chessboard.
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).
For example:
coordinate1 = "a1"
and coordinate2 = "c3"
, return true
because both squares are black.coordinate1 = "a1"
and coordinate2 = "h3"
, return false
because square "a1"
is black and "h3"
is white.Write a function to determine if two chessboard squares have the same color given their coordinates.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem is like having a real chessboard and just visually checking each square's color. We figure out each square's color individually based on its position and then compare if they are the same.
Here's how the algorithm would work step-by-step:
def check_same_color(square_one, square_two):
# Determine the color of the first square
column_one_position = ord(square_one[0]) - ord('a') + 1
row_one = int(square_one[1])
sum_one = column_one_position + row_one
square_one_is_black = sum_one % 2 == 0
# Determine the color of the second square
column_two_position = ord(square_two[0]) - ord('a') + 1
row_two = int(square_two[1])
sum_two = column_two_position + row_two
square_two_is_black = sum_two % 2 == 0
# Compare the colors of the squares
if square_one_is_black == square_two_is_black:
return True
return False
The key is to realize we don't need to know the exact coordinates, just whether the two squares share the same color. We can figure this out using a simple trick: think about how chessboards alternate colors.
Here's how the algorithm would work step-by-step:
def check_chessboard_squares_color(square_one, square_two):
column_one = ord(square_one[0])
row_one = int(square_one[1])
column_two = ord(square_two[0])
row_two = int(square_two[1])
# Check if both column and row are even or odd for the first square
square_one_same_parity = (column_one % 2 == row_one % 2)
# Check if both column and row are even or odd for the second square
square_two_same_parity = (column_two % 2 == row_two % 2)
# If both squares have the same parity, they have the same color
if square_one_same_parity == square_two_same_parity:
return True
else:
return False
Case | How to Handle |
---|---|
Invalid input string length (not equal to 2) | Return false if the string length is not 2, as it's not a valid square representation. |
Invalid input: Non-letter for the column (first character) | Return false if the first character is not a letter between a and h (or A and H), indicating an invalid column. |
Invalid input: Non-digit for the row (second character) | Return false if the second character is not a digit between 1 and 8, indicating an invalid row. |
Both squares are the same square | The problem implies distinct squares but if the same input is provided twice the function should return true (same color). |
Lowercase vs. Uppercase Letters | Convert the column letter to lowercase to ensure case-insensitive comparison and calculation. |
Adjacent squares on the same row | Test for correct color determination when columns are next to each other. |
Adjacent squares on the same column | Test for correct color determination when rows are next to each other. |
Squares at extreme corners (a1, h8, a8, h1) | Verify correctness for squares at the corners of the chessboard. |