Algo-Sim › Low-Level Design
Design Tic-Tac-Toe
Medium · Low-Level Design · step through it in Java, Python and C++ on your own input
Detect a winner in O(1) per move: one running sum per row, column and diagonal, +1 for one player and −1 for the other.
Problem
Design an n × n tic-tac-toe game for two players. move(row, col, player) places the player’s mark on an empty cell and returns the player (1 or 2) if that move completes a full row, column or diagonal, or 0 if nobody has won. No moves are made after a win.
Constraints
- 2 ≤ n ≤ 100
- player is 1 or 2; every move is on an empty cell
- At most n² calls
- move must be O(1), not O(n) or O(n²)
Edge cases to test
- A win on the anti-diagonal
- A win on the last possible move
- n = 1
Follow-up questions
- Why +1 and −1?
- One player adds and the other subtracts, so a line wins exactly when its sum reaches ±n: O(1) per move, O(n) memory.
- How do you detect a draw?
- Count moves; after n² moves with no winner it is a draw.
Solution
Java
class TicTacToe {
private final int n;
private final int[] rows, cols; // player 1 adds 1, player 2 adds -1
private int diag, anti;
public TicTacToe(int n) {
this.n = n;
rows = new int[n];
cols = new int[n];
}
public int move(int row, int col, int player) {
int add = player == 1 ? 1 : -1;
rows[row] += add;
cols[col] += add;
if (row == col) diag += add;
if (row + col == n - 1) anti += add;
if (Math.abs(rows[row]) == n || Math.abs(cols[col]) == n
|| Math.abs(diag) == n || Math.abs(anti) == n)
return player;
return 0;
}
}Python
class TicTacToe:
def __init__(self, n):
self.n = n
self.rows = [0] * n # player 1 adds 1, player 2 adds -1
self.cols = [0] * n
self.diag = self.anti = 0
def move(self, row, col, player):
add = 1 if player == 1 else -1
self.rows[row] += add
self.cols[col] += add
if row == col:
self.diag += add
if row + col == self.n - 1:
self.anti += add
lines = (self.rows[row], self.cols[col], self.diag, self.anti)
if self.n in map(abs, lines):
return player
return 0C++
#include <bits/stdc++.h>
using namespace std;
class TicTacToe {
public:
int n;
vector<int> rows, cols; // player 1 adds 1, player 2 adds -1
int diag = 0, anti = 0;
TicTacToe(int n) {
this->n = n;
rows = vector<int>(n);
cols = vector<int>(n);
}
int move(int row, int col, int player) {
int add = player == 1 ? 1 : -1;
rows[row] += add;
cols[col] += add;
if (row == col)
diag += add;
if (row + col == n - 1)
anti += add;
if (abs(rows[row]) == n || abs(cols[col]) == n
|| abs(diag) == n || abs(anti) == n)
return player;
return 0;
}
};
More Low-Level Design problems
- LRU Cache (Medium)
- LFU Cache (Hard)
- Min Stack (Medium)
- Insert Delete GetRandom O(1) (Medium)
- Design Circular Queue (Medium)
- Flatten Nested List Iterator (Medium)
- Rate Limiter: Token Bucket (Medium)
- Rate Limiter: Sliding Window Log (Medium)
- Logger Rate Limiter (Easy)
- Design Hit Counter (Medium)
- Design Parking System (Easy)
- Design Twitter (Medium)
- Design Snake Game (Medium)
- Vending Machine (State pattern) (Medium)
- Elevator System (LOOK scheduling) (Medium)
- Splitwise (expense sharing) (Medium)
- Movie Ticket Booking (BookMyShow) (Medium)