Free preview

Requirements checklist

  • The question that splits the field: does "legal" include king safety? (Full legality: a move may not leave your own king attacked.)
  • Turn order enforced; input = (from, to) + position; verdict only, no suggestions.
  • Volunteer a staged scope: six pieces, normal moves and captures, full legality now; the rulebook's special cases later.
  • Interactive performance bar — licenses simple algorithms.

The model

UI  ->  isLegal(position, move)
          1. own piece on 'from' (turn rule)
          2. piece's MovementRules accept the move   <- candidates only
          3. simulate on a scratch copy
          4. mover's king not attacked               <- legality, once
        isSquareAttacked(...) REUSES the movement rules
        pins/checks: no special code — step 4 catches them all

Principles, at their decisions

  • Single responsibility: king safety implemented once in the legality layer — never per piece.
  • Open/closed: movement varies by piece behind one interface; the legality algorithm never changes.
  • Don't repeat yourself: attack detection = movement rules asked in reverse.

Complexity facts

  • isLegal: one 64-cell copy + one attack scan — O(64) per query, nothing at interactive rates.
  • isSquareAttacked: O(squares) naive; attack maps make it O(1) at the cost of incremental maintenance — declined until engine-grade throughput is demanded.
  • Game-end check: O(pieces × candidates) simulations, once per completed move.

The five report dimensions, for this problem

  • Requirements & interface — asked what "legal" fully means before designing; staged the scope yourself.
  • Core design & invariants — the two-layer separation stated in words; real position never mutated; one entry point.
  • Extension probe — locate the change in YOUR layers, narrate what moves and what provably doesn't, volunteer the cost.
  • Complexity honesty — every O() with its because; optimizations named with their trigger, not performed on reflex.
  • Communication — the design in sentences before code; wrinkles (the pawn) named unprompted.

Ready? Sit the live mock → — the interviewer will run a twist this chapter deliberately hasn't shown you.

Enjoying the preview?

Create a free account to unlock the rest of this course, the in-browser judge, and live AI mock interviews.

Sign up free to continue