Skip to the page

Timers, games and history · mid

Game state: tic-tac-toe and grid games

Game rounds are state-design tests: keep the board and move history as the only state, derive the current player and the winner, put the rules in pure functions you can test, and handle draws, repeated clicks and timers cleanly.

The notes

What you must know

  • Board as a flat array (index = row × n + col) or 2D array; history of moves for undo/time travel; current player derived from the move count.
  • Winner is derived after each move, never stored separately (it can go out of sync). For N×N with k in a row, check only lines through the last move (O(k)) for big boards.
  • Block clicks on filled cells and after the game ends; detect a draw when the board is full.
  • Memory game: flipped indexes (max 2), matched set, a lock while a mismatch is shown, clear the timeout on reset.
  • Wordle scoring handles duplicate letters: mark exact matches first, then count the remaining letters for 'present'.
  • Minesweeper reveal is BFS from an empty cell; grid lights (Uber) is a queue of clicked cells undone in reverse with timeouts.
  • Separate pure game logic from the UI so you can test it in the console during the round; accessible cells are buttons with labels ('Row 1, column 2, X').

The red pen

Common traps: mistake → fix

  • Mistake: Store winner in state and update it separately

    Fix: it can disagree with the board.

  • Mistake: Hard-code the eight 3×3 lines

    Fix: the N×N follow-up needs a rewrite.

  • Mistake: Wordle: mark every letter that appears anywhere as 'present'

    Fix: wrong with duplicate letters.

  • Mistake: Timeouts not cleared on reset

    Fix: old timers flip cards in the new game.

Easy to hard

Questions interviewers ask, easy to hard

  1. Build tic-tac-toe.
  2. Make it N×N with k in a row.
  3. Add undo / time travel.
  4. Build a memory matching game (Zeta asked this).

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview