DesignTheSystem/dev
EASY Low-Level Design (LLD) 12 automated tests

Design Tic-Tac-Toe (N × N) (Low-Level Design)

Any board size, with a win check that only looks around the last move. Focus: Checking only what a move can change.

DifficultyEASY
Design focusChecking only what a move can change
Automated grading3 public + 9 hidden tests (12 total)
Phase 2 follow-upK in a row
LanguageTypeScript (OOP / Machine Coding)

Problem statement

The board is n × n. X moves first, then O, and so on.

A player wins by filling a whole row, column or diagonal. If the board fills with no winner, it is a draw.

Boards can be large, so after each move only check the lines through that move.

Functional requirements

  • play(row, col) places the current player's mark and returns X_WINS, O_WINS, DRAW or IN_PROGRESS.
  • It throws for a cell outside the board, an occupied cell, or any move after the game ends.
  • board() returns rows as strings, with "." for empty cells.

Required API & starter interface

Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:

type GameStatus = "X_WINS" | "O_WINS" | "DRAW" | "IN_PROGRESS";

class TicTacToe {
  constructor(n: number) {}

  play(row: number, col: number): GameStatus {
    throw new Error("Not implemented");
  }

  board(): string[] {
    throw new Error("Not implemented");
  }
}

Public test cases

Your code runs against 3 public tests plus 9 hidden edge-case tests in a sandbox:

X wins a row (core)

var g = new TicTacToe(3);
g.play(0, 0); g.play(1, 0); g.play(0, 1); g.play(1, 1);
assertEqual(g.play(0, 2), "X_WINS");

Board shows the marks (core)

var g = new TicTacToe(3);
g.play(1, 1); g.play(0, 2);
assertEqual(g.board(), ["..O", ".X.", "..."]);

Occupied cells are refused (edge)

var g = new TicTacToe(3);
g.play(0, 0);
assertThrows(function () { g.play(0, 0); });

Design & structure checks

Beyond passing tests, the grader inspects your class structure for clean object-oriented design:

  • The win check is a separate method: Placing a mark and deciding the result are different jobs. A separate win check is easy to test and to change (as the K-in-a-row change shows).

Phase 2: Change request

In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 12 tests, you unlock the change request: K in a row.

Relevant low-level design lessons

More low-level design problems