Implement a Tic-Tac-Toe game API
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates implementation skills in data structures and algorithmic optimization, focusing on efficient state tracking and constant-time update strategies for game logic on an n×n Tic-Tac-Toe board.
Constraints
- 1 <= n
- 0 <= len(moves) <= n^2
- Each move is a tuple (row, col, player) with 0 <= row, col < n
- player is either 1 or 2
- Moves are valid: no cell is played more than once
Examples
Input: (3, [(0,0,1), (0,2,2), (2,2,1), (1,1,2), (2,0,1), (1,0,2), (2,1,1)])
Expected Output: [0, 0, 0, 0, 0, 0, 1]
Explanation: Player 1 completes the bottom row on the last move.
Input: (1, [(0,0,2)])
Expected Output: [2]
Explanation: On a 1 x 1 board, the first move fills the only row, column, and both diagonals, so player 2 wins immediately.
Input: (4, [(0,1,1), (0,0,2), (1,0,1), (1,1,2), (2,3,1), (2,2,2), (3,2,1), (3,3,2)])
Expected Output: [0, 0, 0, 0, 0, 0, 0, 2]
Explanation: Player 2 fills the main diagonal: (0,0), (1,1), (2,2), (3,3).
Input: (3, [(0,0,1), (0,2,2), (1,0,1), (1,1,2), (2,2,1), (2,0,2)])
Expected Output: [0, 0, 0, 0, 0, 2]
Explanation: Player 2 completes the anti-diagonal: (0,2), (1,1), (2,0).
Input: (3, [(0,1,2), (0,0,1), (1,1,2), (1,0,1), (2,2,2), (2,0,1)])
Expected Output: [0, 0, 0, 0, 0, 1]
Explanation: Player 1 completes column 0 with cells (0,0), (1,0), and (2,0).
Hints
- You do not need to store the full board. Track counts for each row, each column, and the two diagonals.
- Try adding +1 for player 1 and -1 for player 2. If any tracked count reaches n or -n, that player has won.