FastPrepSame-Color Chessboard Positions

Same-Color Chessboard Positions

Pelatro logoPelatro● EasyFULLTIMEONSITE INTERVIEW

Problem statement

Given two positions first and second on a standard 8 x 8 chessboard, return true if they have the same color and false otherwise.

A position uses lowercase algebraic notation: a file from a to h followed by a rank from 1 to 8. Moving one square horizontally or vertically changes the color.

Function

sameColor(first: String, second: String) → boolean

Examples

Example 1

first = "a1"second = "c3"return = true

Moving from a1 to c3 takes two horizontal and two vertical steps. The four color changes return to the starting color, so the answer is true.

Example 2

first = "a1"second = "a2"return = false

a1 and a2 are one vertical step apart. Their colors differ, so the answer is false.

Constraints

  • first.length = second.length = 2.
  • Each position consists of one lowercase file from a through h, followed by one rank from 1 through 8.
  • Both positions are valid squares on a standard 8 x 8 chessboard. They may be equal.

More Pelatro problems

See Pelatro hiring insights
Saved
public boolean sameColor(String first, String second) {
    // Write your solution here.
}
first"a1"
second"c3"
expectedtrue
Checking account…