Tic Tac Toe

Classic Board Game

Design a two-player Tic Tac Toe with move validation, constant-time win detection, a draw rule and a scoreboard that follows many games.

Overview

Two players take turns marking cells of a 3 by 3 grid. The first to fill a row, a column or a diagonal wins; a full grid with no line is a draw. Everyone knows the rules, so the interviewer spends the time on how you split the work between classes, how you validate a move, and how you detect a win without scanning the whole board.

The problem is a warm-up, but it separates tidy designs from tangled ones fast. A tidy design has a Board that only knows about cells, a Game that only knows about turns and rules, immutable Players, and a Scoreboard that learns about results without the Game knowing what a scoreboard is. A tangled one puts everything in one class with a nest of if statements.

A strong answer also shows the constant-time win check: keep a signed counter per row, per column and per diagonal, and test only the four counters the last move touched. Then it shows that the same code plays a 4 by 4 board with no change but a parameter.

Scoping the problem

Before writing code, ask these questions; each answer changes the design a little.

  • Is the board size fixed at 3, or N by N? A parameter costs nothing and shows generality. The usual answer is N by N, with a default of 3.
  • Two players only? More players would need a list and a turn index. The usual answer is two: X and O.
  • What happens on a bad move? You can return false or raise; either way the turn must not pass. The usual answer is to raise, and the turn stays.
  • Track results across games? Usually yes, which adds a scoreboard and a way to tell it.
  • Undo, replay, an AI opponent? Each is a follow-up with its own design, not part of the core.

Functional requirements

  • Two players, X and O, alternate turns on an n by n board (default 3)
  • play(row, col) places the current player's mark; an off-board cell, a taken cell, or a move after the game ends is refused and the turn does not pass
  • A move that completes a row, column or diagonal wins; a move that fills the last cell with no line is a draw
  • The game reports its status (in progress, won, draw) and the winner
  • Listeners such as a Scoreboard are told when a game ends and count wins and draws across games

Non-functional requirements

  • Win detection is O(1) per move through signed line counters
  • Board size is a parameter; no code names the number 3
  • Player is immutable (a frozen dataclass), so a game cannot swap marks mid-way
  • Game does not depend on Scoreboard; consumers register as listeners
  • Every rule is covered by an assert in the demo, including each kind of invalid move

Interview questions

  • How would you support a bigger board, or a rule like four in a row on a 6 by 6 board?
  • Why not check for a win by scanning the board? It is only nine cells.
  • Where do you validate a move, and what should happen on a bad one?
  • How would you add an unbeatable computer player?
  • How do you add undo?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio