FastPrepValidate a Chess Bishop, Rook, or Queen Move
Problems/Validate a Chess Bishop, Rook, or Queen Move

Validate a Chess Bishop, Rook, or Queen Move

Glean logoGlean● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In this phone screen the chess-backend question was cut down to one job: decide whether a bishop move between two squares, each given by x and y, is legal. The checks covered the diagonal shape, staying in place, and pieces in the way. A follow-up asked how the same path check could also serve the rook and the queen.

The report does not give the board format, how colors are marked, or what happens when the target square holds a piece. Here the board is 8 strings read as board[y][x]. The start square always holds a bishop, rook or queen of either color. Uppercase pieces are white and lowercase pieces are black. A piece may capture a piece of the other color but never land on its own. Turns, check and all other chess rules are ignored. The rook and queen follow-up is part of the task.

Our version is about 85% the same as the reported question.

You are writing the backend for a two-player chess game, and the server must reject illegal moves. Write the move check for the pieces that slide across the board: the bishop, the rook, and the queen.

The board is given as board, 8 strings of 8 characters. A position is written [x, y], where x is the column and y is the row, so the square at [x, y] is character x of board[y]. Both start at 0, and row 0 is the first string.

A '.' is an empty square. A letter is a piece: K king, Q queen, R rook, B bishop, N knight, and P pawn. Uppercase letters are white pieces and lowercase letters are black pieces.

The square start holds a bishop, rook, or queen. Return true if that piece can move from start to end, and false otherwise. A move is valid when all of these hold:

  1. The piece moves: end is a different square from start.
  2. The move has the right shape. A bishop moves diagonally, so the column and the row change by the same amount. A rook moves in a straight line, so the column or the row stays the same. A queen can move either way.
  3. Every square strictly between start and end is empty. A piece cannot jump over another piece.
  4. The square end is empty or holds a piece of the other color, which is captured. A piece cannot land on a piece of its own color.

Only the moving piece's own movement counts. Ignore whose turn it is, check, and every other rule of chess.

Function

isValidMove(board: String[], start: int[], end: int[]) → boolean

Examples

Example 1

board = ["........","........","........","........","........","........","........","..B....."]start = [2,7]end = [5,4]return = true
board
012345670........1........2........3........4........5........6........7..B.....

The white bishop is at [2, 7], column 2 of the bottom row. To reach [5, 4] it moves 3 columns right and 3 rows up, so the move is diagonal. The squares in between, [3, 6] and [4, 5], are empty, and the target square is empty. The move is valid.

Example 2

board = ["........","........","........","........","........","....p...","........","..B....."]start = [2,7]end = [5,4]return = false
board
012345670........1........2........3........4........5....p...6........7..B.....

The same bishop move, but a black pawn stands on [4, 5], one of the squares in between. A bishop cannot jump over a piece, so the move is not valid.

Example 3

board = ["R.......","........","........","........","........","n.......","........","........"]start = [0,0]end = [0,5]return = true
board
012345670R.......1........2........3........4........5n.......6........7........

The white rook at [0, 0] moves straight down column 0 to [0, 5]. Rows 1 to 4 of that column are empty. The target holds a black knight, a piece of the other color, so the rook captures it. The move is valid.

Example 4

board = ["........","....n...","........","...Q....","........","........","........","........"]start = [3,3]end = [4,1]return = false
board
012345670........1....n...2........3...Q....4........5........6........7........

The white queen at [3, 3] would move 1 column right and 2 rows up. That is neither a straight line nor a diagonal, so a queen cannot make this move, even though the target holds a black knight that could otherwise be captured.

Constraints

  • board.length == 8 and board[i].length == 8
  • Every character of board is one of .KQRBNPkqrbnp.
  • start.length == 2 and end.length == 2
  • 0 <= start[i], end[i] <= 7
  • The square at start holds a bishop, rook, or queen of either color.
All Glean interview questionsSee Glean hiring insights
Saved
class Solution {
    public boolean isValidMove(String[] board, int[] start, int[] end) {
        // Write your code here.
    }
}
Checking account…