The Design Tic-Tac-Toe coding problem asks you to implement a game on an board. Two players take turns placing their marks (1 or 2). A player wins if they complete a full row, column, or either of the two main diagonals. You need to implement a move function that returns the winner (0 if no winner yet).
This is a classic "Medium" question at companies like Apple, Microsoft, and Amazon. It tests whether you can optimize a simulation. A naive solution checks the entire row, column, and diagonals for every move (). An optimized solution should determine the winner in time, which evaluations your ability to use "Counters" to track game state.
The Simulation design pattern with Counters is used here. You don't actually need to store the board.
rows[n] and cols[n].diag1 and diag2.Board.
rows[0] = 1, cols[0] = 1, diag1 = 1. Winner: 0.rows[1] = -1, cols[1] = -1, diag1 = 0. Winner: 0.rows[0] = 2, cols[1] = 0. Winner: 0.rows[0] = 3. Winner! Returns 1.row + col == n - 1).This problem is a masterclass in "State Reduction." When an interviewer asks you to design a game, always ask: "Do I need to store the whole board, or just the summary of the board?" Summary-based designs are almost always more efficient.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Equal Row and Column Pairs | Medium | Solve | |
| Design Memory Allocator | Medium | Solve | |
| Simple Bank System | Medium | Solve | |
| Find Winner on a Tic Tac Toe Game | Easy | Solve | |
| Design Snake Game | Medium | Solve |