Implement Generalized Tic-Tac-Toe
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
# Implement Generalized Tic-Tac-Toe
Implement a `TicTacToe` class for two players on an `m` by `n` board with a configurable winning length `k`.
```text
TicTacToe(m, n, k)
move(row, col, player) -> int
```
Players are identified by `1` and `2`. A move places the player's mark in an empty cell and returns `0` if there is no winner, `1` if player 1 has won, or `2` if player 2 has won. A player wins by occupying at least `k` consecutive cells in one horizontal, vertical, main-diagonal, or anti-diagonal line that includes the newly placed mark.
## Constraints
- `1 <= m, n <= 200`
- `1 <= k <= max(m, n)`
- Calls use valid player IDs, in-bounds coordinates, and previously empty cells.
- After a winner is returned, no more moves will be made.
- Aim for work proportional to the affected row, column, and diagonals rather than scanning the entire board.
## Examples
For `TicTacToe(3, 4, 3)`, the moves `(1, 0, 1)`, `(0, 0, 2)`, `(1, 1, 1)`, `(0, 1, 2)`, `(1, 2, 1)` return `0, 0, 0, 0, 1`.
## Clarifications
Consecutive means no gap between marks. A line longer than `k` also wins. If `k` exceeds the length of a particular direction, that direction simply cannot produce a win.
## Hints
Only lines through the newest mark can change from non-winning to winning.
## Extensions
- How would you support undo?
- Can you reduce per-move work when `k` is close to a board dimension?
- How would the design change for many concurrent games?
Quick Answer: Implement generalized Tic-Tac-Toe for two players on a rectangular board with a configurable consecutive-mark winning length. Handle horizontal, vertical, and both diagonal wins through the newest move, including short directions and winning lines longer than the target.