FastPrepOnline Chess Move Validation

Online Chess Move Validation

OpenAI logoOpenAI● HardFULLTIMENEW GRADPHONE SCREEN

Problem statement

Simulate move attempts from the standard initial chess position. White moves first. Each string in moves contains an origin square followed by a destination square in lowercase file-rank notation, such as e2e4.

For each attempt, append LEGAL or ILLEGAL to the result. An illegal attempt leaves the board and turn unchanged. An accepted move changes the board and passes the turn.

Use ordinary movement for kings, queens, rooks, bishops, knights, and pawns. Sliding pieces may not jump. A pawn may move one empty square forward, move two empty squares from its starting rank when the intermediate square is empty, or capture one opposing piece diagonally forward. A move may not capture a king or leave the moving side's king in check. Castling, en passant, promotion, clocks, draw rules, and checkmate adjudication are outside this exercise; a pawn move onto the last rank is illegal.

After all attempts, append eight board rows to the result in rank 8 through rank 1 order. Use uppercase letters for White pieces, lowercase letters for Black pieces, and . for an empty square. The returned array therefore contains moves.length + 8 strings.

Function

playChess(moves: String[]) → String[]

Examples

Example 1

moves = ["e2e4","e7e5","g1f3"]return = ["LEGAL","LEGAL","LEGAL","rnbqkbnr","pppp.ppp","........","....p...","....P...",".....N..","PPPP.PPP","RNBQKB.R"]

Both king pawns advance, then White's knight jumps from g1 to f3. The eight trailing strings encode the resulting board.

Example 2

moves = ["e2e5","e2e4","e7e5","e4e5"]return = ["ILLEGAL","LEGAL","LEGAL","ILLEGAL","rnbqkbnr","pppp.ppp","........","....p...","....P...","........","PPPP.PPP","RNBQKBNR"]

A pawn cannot advance three squares. After the two legal replies, White's pawn cannot move forward into Black's occupied pawn.

Example 3

moves = []return = ["rnbqkbnr","pppppppp","........","........","........","........","PPPPPPPP","RNBQKBNR"]

With no attempts, the result is the standard initial board.

Constraints

  • 0 <= moves.length <= 200.
  • Every move is exactly four characters and matches [a-h][1-8][a-h][1-8].
  • The origin and destination squares in one attempt are different.

More OpenAI problems

See OpenAI hiring insights
Saved
public String[] playChess(String[] moves) {
  // Write your code here.
}
moves["e2e4","e7e5","g1f3"]
expected["LEGAL", "LEGAL", "LEGAL", "rnbqkbnr", "pppp.ppp", "........", "....p...", "....P...", ".....N..", "PPPP.PPP", "RNBQKB.R"]
Checking account…