Design chess 00:00:00 Make a prediction You will separate geometric move generation from king safety and prove that trying a move leaves the original board intact when rejected. A rook can move along an open file, but is shielding its own king from an opposing rook. May it move sideways just because the destination square is reachable? No. Geometric reach gives a candidate move. Apply it temporarily, check whether the king would be attacked, then restore the position if it is illegal. 00:00:32 Tower, king and herald A herald records every move in a castle exercise. A tower steps aside for a trial and uncovers the king. The herald restores both figures before considering the next move and keeps a record of what was captured. Generate candidate moves by each piece's rule. Legal move checking and undo must preserve the entire position, including special rights that a board picture alone may omit. Chess in a low-level design round is the object model of a board game with strict rules: six kinds of pieces, turns, special moves, and an end that depends on a piece that does not move. It is one of the few popular exercises where a class hierarchy is really right, so it shows whether you can tell when inheritance is earned. 00:01:20 A legal shape can still expose the king A rook may geometrically move along an open file. But if moving it exposes its own king to attack, the move is illegal. Generate the candidate using piece movement rules, apply it to a temporary position, and check whether the moving side's king is attacked. Keep the move only if the king is safe, then commit state such as side to move. Undoing a trial must also restore captured pieces and special rights. Move the bishop diagonally on a temporary board and inspect the resulting attack line. The move can be locally legal for a bishop while exposing its own king. Discard that tentative board on rejection. Testing piece movement alone misses this board-level rule. 00:02:07 Distinguish checkmate from stalemate In both positions the side to move has no legal move. If its king is under attack, it is checkmate. If its king is safe, it is stalemate. Reuse the legal-move generator rather than writing a separate shortcut based on king movement alone. Evaluate two positions with no legal moves. If the current king is under attack, the result is checkmate. If the king is safe, the result is stalemate. The move count alone cannot distinguish them. Use the same current-player perspective for both checks. Never decide stalemate merely because the king has no adjacent safe square. Another piece might still have a legal move. Separate a move generator and a legality check first; optimize with checks and pins only after the basic apply-and-undo tests are reliable. Stockfish applies a candidate move while recording the information needed to undo it during search. The Python chess library also maintains a move stack. Undoing a move must restore the entire previous position, including rights that affect future legal moves. Merely putting the moved piece back on its square can be insufficient. 00:03:19 Try an interview follow-up Why would you avoid storing a flag on each piece saying whether it has moved? Undo has to reset it, a loaded position has no history, and repetition has to compare it. Rights as a set of corners in the position are one field in one place. Corners are also lost when the rook is captured. A search engine castles, evaluates a position and then undoes that move. What has to return besides the king's square? Restore both pieces, the side to move and the castling rights recorded before the move. Search is only trustworthy if the restored position has the same legal moves as the starting one. Comparing the board picture alone can miss lost rights. 00:04:07 What to take with you Pieces say which squares they hit and know nothing else. The board alone knows who stands where. The position adds turn, castling rights, the en passant square and a counter, and can apply a move and take it back exactly. Rules make candidates and keep those after which the king is not hit. The game checks the turn, plays, judges the end and tells its listeners. Check is a question, not a state.