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.
| Difficulty | EASY |
|---|---|
| Design focus | Checking only what a move can change |
| Automated grading | 3 public + 9 hidden tests (12 total) |
| Phase 2 follow-up | K in a row |
| Language | TypeScript (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
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
var g = new TicTacToe(3);
g.play(1, 1); g.play(0, 2);
assertEqual(g.board(), ["..O", ".X.", "..."]);
Occupied cells are refused
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
Systems that move step by step
Model a system that changes over time with a clear step rule.
From a problem to classes
Turn a problem statement into a first list of classes and methods.
Hide state behind methods
Protect an object's rules by keeping its data private.
More low-level design problems
Design Parking Lot
Park vehicles in the right size of spot, issue tickets and charge on exit.
Design Vending Machine
Take coins, sell products, give change, and refuse what a state does not allow.
Design Shopping Cart
Already solved, with comments: add items, apply tier discounts, then coupons. Run it and submit it to see how grading works.
Design Snake and Ladder
A board game where the board, the players and the rules for a turn are separate pieces.