Backend for a game of checkers
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in SpaceXAI's onsite applied coding round, last asked Feb 2026. It tests object-oriented design, game and simulation.
Build the backend of a two-player checkers game in limited time: the board state, legal moves, captures, promotion to king and the end of the game. The interviewer cares more about a working core and a data model you can explain than about polish.
Interface
Unless you agree on a different interface with the interviewer, assume standard 8×8 checkers:
-
Game(): a new game. Rows and columns run 0 to 7, row 0 at the top. Pieces stand only on squares where(row + col)is odd.BLACKfills rows 0 to 2 and moves down (toward row 7);WHITEfills rows 5 to 7 and moves up.BLACKmoves first. -
board() -> list[list[str]]:"."empty,"b"/"w"a man,"B"/"W"a king. -
current_player() -> str:"BLACK"or"WHITE". -
legal_moves() -> list[list[tuple[int, int]]]: every legal move for the current player, each as a path of squares from the starting square. A plain move has two squares; a capture lists every landing square of its jumps. -
move(path: list[tuple[int, int]]) -> None: plays the move, removes captured pieces, promotes a man that reaches the far row, and passes the turn. RaisesValueErrorfor an illegal move and leaves the state unchanged. -
winner() -> str | None: the player whose opponent has no pieces or no legal move, elseNone.
Rules: men move one square diagonally forward; kings move one square diagonally in any direction. A capture jumps a diagonally adjacent enemy piece onto the empty square behind it. If any capture is available, the player must capture, and must keep jumping with the same piece while it can. A man that reaches the far row becomes a king and its move ends there.
Example
g = Game()
g.current_player() -> "BLACK"
len(g.legal_moves()) -> 7
# (2,1)->(3,0) (2,1)->(3,2) (2,3)->(3,2) (2,3)->(3,4)
# (2,5)->(3,4) (2,5)->(3,6) (2,7)->(3,6)
g.move([(2, 1), (3, 0)])
g.current_player() -> "WHITE"
g.move([(5, 0), (4, 1)])
g.move([(3, 0), (5, 2)]) -> ValueError # (5,2) is occupied, no jump here
Constraints
- A game is short (well under 200 moves), so clarity beats micro-optimization.
-
movemust validate against the same rules aslegal_moves, not a separate copy of them.
Clarifying questions
- Are captures mandatory, and must a multi-jump be completed?
- Do kings move one square or any distance along a diagonal (flying kings)?
- Does reaching the far row in the middle of a multi-jump end the move?
- Is a draw needed (repetition, a move limit)?
- Is this a library, or a service with players, game ids and an API?
What a strong answer covers
- A board model (8×8 array or 32 playable squares) and piece encoding that make direction and king logic simple.
- Move generation with a DFS for multi-jumps, the forced-capture rule applied across all pieces, and promotion.
-
moveimplemented as "is this path inlegal_moves()", so rules live in one place. - Win detection by no pieces or no legal moves.
- Tests for forced captures, double jumps, promotion, kings moving backward and illegal moves.
Follow-ups
- Turn it into an online service: game creation, joining, an API for moves, and storing games.
- Two players submit moves at the same moment or a client retries: how do you keep turns authoritative and moves idempotent?
- Add undo or a move history that can replay a game.
- Add a simple computer opponent with minimax.
Where it's asked
- SpaceXAI · Onsite applied coding · Last asked Feb 2026Two or three rounds, in person (Palo Alto for many roles)
What it tests
object-oriented design · game · simulation
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
