PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

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.

  • hard
  • Databricks
  • Coding & Algorithms
  • Software Engineer

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.

Implement a generalized Tic-Tac-Toe engine for two players on an `m x n` board with a configurable winning length `k`. The interview states the problem as a class: ```text TicTacToe(m, n, k) move(row, col, player) -> int ``` For grading, that class is driven by a single function. `generalized_tic_tac_toe(m, n, k, moves)` constructs a board with `m` rows and `n` columns and winning length `k`, then replays `moves` in order. Element `i` of `moves` is a triple `[row, col, player]` and stands for exactly one call `move(row, col, player)`. Return the list of the values those calls return, one per move, in move order. ## Rules - Players are identified by `1` and `2`. A move places that player's mark in cell `(row, col)`, which is empty immediately before the move. - A move returns `0` if nobody has won, `1` if player 1 has won, or `2` if player 2 has won. - A player wins when, after their mark is placed, they occupy at least `k` consecutive cells of one line through the newly placed cell. The four line directions are horizontal (same row), vertical (same column), main diagonal (down-right), and anti-diagonal (down-left). - "Consecutive" means no gap: every cell of the run holds that player's mark. A run longer than `k` also wins. If `k` is greater than the longest line this board has in some direction, that direction can never produce a win. - Rows are indexed `0` to `m - 1` from the top, columns `0` to `n - 1` from the left. - Players do not have to alternate; each move names the player who makes it. ## Output Return a list of integers whose length is exactly `len(moves)`, where element `i` is the value returned by move `i`. Every element is `0`, `1`, or `2`. When `moves` is empty, return an empty list. The answer is fully determined by the input, so there is no ordering or tie-breaking freedom anywhere. ## Examples **Example 1** ```text Input: m = 3, n = 4, k = 3, moves = [[1, 0, 1], [0, 0, 2], [1, 1, 1], [0, 1, 2], [1, 2, 1]] Output: [0, 0, 0, 0, 1] ``` Player 1 builds row 1 and player 2 builds row 0. After the fifth move player 1 holds `(1,0)`, `(1,1)` and `(1,2)` — three consecutive cells in row 1 — so that move returns `1`. Before it, neither player has more than two in a row anywhere. **Example 2** ```text Input: m = 1, n = 5, k = 3, moves = [[0, 0, 1], [0, 1, 1], [0, 3, 1], [0, 4, 1], [0, 2, 1]] Output: [0, 0, 0, 0, 1] ``` After four moves player 1 holds columns 0, 1, 3 and 4: two runs of length 2 separated by the still-empty column 2, so nothing wins. The fifth move fills column 2 and merges them into one run of length 5, which is at least `k = 3`, so it returns `1`. ## Guidance Only lines through the cell you just filled can turn from non-winning into winning, so aim for work proportional to the affected row, column and two diagonals rather than rescanning the whole board on every move. This is a performance goal, not part of the returned answer.

Constraints

  • 1 <= m <= 200
  • 1 <= n <= 200
  • 1 <= k <= max(m, n)
  • 0 <= len(moves) <= m * n
  • Each moves[i] has exactly 3 elements: 0 <= moves[i][0] < m, 0 <= moves[i][1] < n, and moves[i][2] is 1 or 2.
  • Every move targets a cell that is empty at that point, so no two moves name the same cell.
  • No move is made after a move that returned a winner (1 or 2).
  • Players need not alternate turns.
  • Every input value is at most 200 and every returned value is 0, 1 or 2, so all arithmetic fits in a 32-bit signed integer; no language needs 64-bit types.

Examples

Input: (3, 4, 3, [[1, 0, 1], [0, 0, 2], [1, 1, 1], [0, 1, 2], [1, 2, 1]])

Expected Output: [0, 0, 0, 0, 1]

Input: (3, 3, 3, [])

Expected Output: []

Hints

  1. The board changes by one cell per move, and marks are never removed. Only lines that pass through the cell you just filled can go from non-winning to winning, so there is never a reason to rescan cells far away from it.
  2. For a single direction, the run through the new cell extends both ways. Walking outward along one of the two opposite steps only, and stopping at the first cell that is not the current player's, misses half the run.
  3. Compare the counted run length with `>=`, not `==`: a move can complete a run that is longer than k in one step.
Last updated: Aug 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 9,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Build a Constant-Time Snapshot Set Iterator - Databricks (hard)
  • Find the Earliest Anagram Window - Databricks (medium)
  • Design an n x n Tic-Tac-Toe Game - Databricks (medium)
  • IPv4 CIDR Range Membership Queries - Databricks (medium)