Design Tic-Tac-Toe - Problem

You're building an intelligent Tic-Tac-Toe game that can handle boards of any size! ๐ŸŽฏ

Your task is to design a TicTacToe class that can efficiently determine winners on an n ร— n board. The game follows these rules:

  • ๐Ÿ“ Players alternate turns placing their marks (Player 1 and Player 2)
  • ๐ŸŽฏ A player wins by getting n marks in a row - horizontally, vertically, or diagonally
  • โœ… All moves are guaranteed to be valid (no need to check bounds or occupied cells)
  • ๐Ÿ Once someone wins, no more moves are allowed

Your Challenge: Implement the class efficiently! The naive approach of checking the entire board after each move would be O(nยฒ) - but can you do better? ๐Ÿš€

Class Interface:

TicTacToe(int n)     // Initialize nร—n board
int move(row, col, player)  // Make move, return winner (0=none, 1=player1, 2=player2)

Input & Output

example_1.py โ€” Basic 3ร—3 Game
$ Input: TicTacToe(3) move(0, 0, 1) โ†’ 0 move(0, 2, 2) โ†’ 0 move(2, 2, 1) โ†’ 0 move(1, 1, 2) โ†’ 0 move(2, 0, 1) โ†’ 0 move(1, 0, 2) โ†’ 2
โ€บ Output: Player 2 wins on the last move
๐Ÿ’ก Note: Player 2 gets three in the first column: (0,2)โ†’X, (1,0)โ†’X, (1,1)โ†’X, forming a winning line vertically in column 0.
example_2.py โ€” Diagonal Win
$ Input: TicTacToe(3) move(0, 0, 1) โ†’ 0 move(0, 1, 2) โ†’ 0 move(1, 1, 1) โ†’ 0 move(0, 2, 2) โ†’ 0 move(2, 2, 1) โ†’ 1
โ€บ Output: Player 1 wins with main diagonal
๐Ÿ’ก Note: Player 1 achieves victory by occupying the main diagonal: positions (0,0), (1,1), and (2,2).
example_3.py โ€” Larger Board
$ Input: TicTacToe(4) move(0, 0, 1) โ†’ 0 move(1, 1, 2) โ†’ 0 move(0, 1, 1) โ†’ 0 move(1, 2, 2) โ†’ 0 move(0, 2, 1) โ†’ 0 move(1, 3, 2) โ†’ 0 move(0, 3, 1) โ†’ 1
โ€บ Output: Player 1 wins entire top row
๐Ÿ’ก Note: On a 4ร—4 board, Player 1 fills the entire first row with 4 consecutive marks to win.

Visualization

Tap to expand
Evolution: Board Scanning โ†’ Smart CountersโŒ Brute Force O(n)Check entire boardafter each moveScan 4n positionsevery single timeโœ… Smart Counters O(1)Track running totalsUpdate 2-4 countersInstant winner detectionMathematical eleganceExample: 3ร—3 BoardBrute Force:Check 12 positionsevery moveSmart Counters:8 counters total(3 rows + 3 cols + 2 diags)Update 2-4, check instantly!
Understanding the Visualization
1
Traditional Approach
After each move, scan entire board checking every possible winning line
2
Smart Realization
We only need to track progress toward victory, not the full board state
3
Counter Magic
Each counter represents progress: +1 for Player 1, -1 for Player 2
4
Instant Victory
When any counter hits ยฑn, we have an immediate winner!
Key Takeaway
๐ŸŽฏ Key Insight: Instead of asking 'What's on the board?', ask 'How close is each player to winning?' - this transforms an O(n) problem into O(1) magic!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Each move only updates constant number of counters and checks for winners instantly

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

We store 2n+2 counters (n rows + n columns + 2 diagonals)

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค n โ‰ค 100
  • player is either 1 or 2
  • 0 โ‰ค row, col < n
  • (row, col) are valid coordinates of an empty cell
  • At most n2 calls will be made to move
Asked in
Google 42 Microsoft 38 Amazon 35 Meta 28
67.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen