Back to challenges
Easy 0 solved 0% acceptance

Chess Square Color

Determine the color of a square on an 8 by 8 board.

What you'll learn

  • Turn a square name into row and column numbers
  • Use parity to decide the color
  • Convert letters to numbers

Problem

Use a board whose top-left square is white. Rows and columns start at 0 at the top left. Adjacent squares alternate colors. Return empty text for a position outside the board.

Implement chess_square_color(column, row).

Input

column: The column, counted from the left starting at 0.

row: The row, counted from the top starting at 0.

Output

Return white, black, or an empty string for invalid coordinates.

Constraints

−10 ≤ column, row ≤ 20. Valid board coordinates are 0 through 7.

Examples

Example 1
Input:
row = 3
column = 2

Expected output:
expected = "black"
Input:
long long row = 3;
long long column = 2;

Expected output:
std::string expected = "black";
Input:
long row = 3;
long column = 2;

Expected output:
string expected = "black";

Explanation

Five steps from the white corner change color an odd number of times.

Example 2
Input:
row = 0
column = 8

Expected output:
expected = ""
Input:
long long row = 0;
long long column = 8;

Expected output:
std::string expected = "";
Input:
long row = 0;
long column = 8;

Expected output:
string expected = "";

Explanation

Column 8 is outside the valid range 0 through 7.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Square color alternates with moves across rows and columns, so only the coordinate pattern matters.

Hint 2

A parity check on row and column can tell you which color to return.

Hint 3

Return empty text immediately when either coordinate is outside the 0 to 7 board range.

Expected complexity

O(1) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.