Online Chess Move Validation
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.
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.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.