DesignTheSystem/dev
HARD Low-Level Design (LLD) 14 automated tests

Design Chess Move Rules (Low-Level Design)

Generate and play legal piece moves, with each kind of piece owning its own movement. Focus: Polymorphism: each piece knows how it moves.

DifficultyHARD
Design focusPolymorphism: each piece knows how it moves
Automated grading3 public + 11 hidden tests (14 total)
Phase 2 follow-upCheck and checkmate
LanguageTypeScript (OOP / Machine Coding)

Problem statement

Squares are written a1 to h8 (file then rank). Pieces are two characters: colour w or b, then K, Q, R, B, N or P, for example "wN" or "bQ".

The board starts empty; tests place pieces. White moves first.

Moves follow normal chess movement: rooks, bishops and queens slide until blocked (they may capture the first enemy piece), knights jump, kings step one square. Pawns move one square forward, two from their starting rank if both squares are empty, and capture one square diagonally forward. White pawns move up the ranks, black pawns down. A pawn reaching the last rank becomes a queen. No castling or en passant.

For now, ignore check: a move is allowed even if it leaves your own king attacked.

Functional requirements

  • place(square, piece) and pieceAt(square) (null when empty). Invalid squares or pieces throw.
  • legalMoves(square) returns target squares sorted alphabetically (empty list for an empty square).
  • move(from, to) plays the move for the side whose turn it is and returns the captured piece or null. Wrong side, empty square or an illegal target throws.
  • turn() is "w" or "b".

Required API & starter interface

Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:

type Color = "w" | "b";

class ChessGame {
  place(square: string, piece: string): void {
    throw new Error("Not implemented");
  }

  pieceAt(square: string): string | null {
    throw new Error("Not implemented");
  }

  legalMoves(square: string): string[] {
    throw new Error("Not implemented");
  }

  /** Returns the captured piece, or null */
  move(from: string, to: string): string | null {
    throw new Error("Not implemented");
  }

  turn(): Color {
    throw new Error("Not implemented");
  }
}

Public test cases

Your code runs against 3 public tests plus 11 hidden edge-case tests in a sandbox:

Knight jumps (core)

var g = new ChessGame();
g.place("b1", "wN");
assertEqual(g.legalMoves("b1"), ["a3", "c3", "d2"]);

Rook stops at blockers and captures (core)

var g = new ChessGame();
g.place("a1", "wR"); g.place("a3", "wP"); g.place("c1", "bN");
assertEqual(g.legalMoves("a1"), ["a2", "b1", "c1"]);

White moves first (edge)

var g = new ChessGame();
g.place("e7", "bP");
assertEqual(g.turn(), "w");
assertThrows(function () { g.move("e7", "e6"); });

Design & structure checks

Beyond passing tests, the grader inspects your class structure for clean object-oriented design:

  • Each kind of piece has its own class (or move strategy): Movement is what differs between pieces. One class per kind keeps each rule small and lets you add fairy pieces without touching the others.
  • No big switch on the piece type: A switch on piece type has to be edited for every new rule. Polymorphism puts each rule with its piece.

Phase 2: Change request

In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 14 tests, you unlock the change request: Check and checkmate.

Pair with High-Level System Design

Ready to scale Chess Move Rules across distributed servers? Try the matching HLD simulator problem: Design Online Chess.

Relevant low-level design lessons

More low-level design problems