Design Tic-Tac-Toe

Beginner · 18 min read · Low-Level Design, Case Studies: Core

Board, players, O(1) win detection and an extensible game loop.

Tic-tac-toe looks trivial, which is exactly why it's asked: the interviewer is grading structure and extensibility, not the game. Can your design handle an N×N board, K-in-a-row, more than two players, and bots — without rewriting? And can you detect a win in O(1) per move instead of scanning the board?

What the interviewer expects

The full design in this lesson is longer than anything you'd write in one round. It's there so you understand the edge cases and can answer follow-up questions. In the round itself, aim for this:

1. Requirements

2. Class design

Key choices:

3. Implementation

Scanning the whole board after each move is O(N²). Instead, keep per-player counters for every row, column and the two diagonals; a move increments four counters at most, and a counter reaching N is a win.

4. Patterns used

5. Extensions

Interview questions

  • How do you detect a win in O(1) per move?
  • How would your design change for Connect Four or Gomoku?

Further reading

Key takeaways

  • Keep Board, WinDetector, Player strategy and Game orchestration as separate responsibilities.
  • Players differ only in how they choose moves — a strategy.
  • Counters give O(1) win detection for N-in-a-row; directional checks handle K-in-a-row.
  • Validate turn order, bounds, occupancy and game-over state explicitly.

Previous: Design an Elevator System · Next: Design an LRU Cache