PythonStudio
Interview Problems

Chess Game

hard · rules engine, move validation

Brief-reported interview attribution (unverified): Microsoft, Amazon, Google

How to approach an LLD round

Implement a simplified chess game on an 8×8 board. Rows 0..7 (top to bottom), columns 0..7 (left to right). Row 0 is Black's back rank, row 7 is White's. White pawns move up (decreasing row), Black pawns move down (increasing row). Pieces are strings: "wP", "wN", "wB", "wQ", "wK", "wR" for White, "b…" for Black, "" for an empty square. There is **no check or checkmate** in this version: a player wins by capturing the opponent's king. Castling, en passant, and promotion are not implemented (they are listed as follow-ups on the page). The constructor accepts an optional starting board for testing.

class ChessGame:
    def __init__(self, board=None):
        # board: optional 8x8 list of lists; default is the standard starting position
    def move(self, from_sq, to_sq):
        # from_sq, to_sq: (row, col) tuples
        # -> "ok" | "invalid" | "white wins" | "black wins"
    def board(self):
        # -> 8x8 list of lists (a copy)
    def current_player(self):
        # -> "white" | "black"
    def status(self):
        # -> "in progress" | "white wins" | "black wins"

Rules. A move is "invalid" if the game is over, the coordinates are out of bounds, the from-square is empty, it is not that colour's turn, the destination holds a friendly piece, or the piece cannot move that way. Pawn: one step forward if empty; two steps from its starting row if both squares are empty; one diagonal step forward only onto an enemy piece. Knight: an L (2+1), jumping over pieces. Bishop: diagonal, path clear. Rook: straight, path clear. Queen: bishop or rook. King: one square in any direction. Capturing the king wins immediately.

Coordinates are authoritative: (6, 4) → (5, 4) is a one-square pawn move. This exercise deliberately is not full legal chess.

Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.

Public demo

# Public demo: run after implementing the class API above.
g = ChessGame()
print(g.board()[0])
print(g.board()[7])
print(g.board()[6])
print(g.board()[1])
print(g.current_player())

Starter code

class ChessGame:

    def __init__(self, board=None):
        """board: optional 8x8 list of lists; default is the standard starting position"""
        pass

    def board(self):
        """-> 8x8 list of lists (a copy)"""
        pass

    def current_player(self):
        '''-> "white" | "black"'''
        pass

    def status(self):
        '''-> "in progress" | "white wins" | "black wins"'''
        pass

    def move(self, from_sq, to_sq):
        '''from_sq, to_sq: (row, col) tuples -> "ok" | "invalid" | "white wins" | "black wins"'''
        pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler