PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Databricks

Implement Generalized Tic-Tac-Toe

Last updated: Jul 28, 2026

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.

Related Interview Questions

  • Build a Constant-Time Snapshot Set Iterator - Databricks (hard)
  • IPv4 CIDR Range Membership Queries - Databricks (medium)
  • Design an n x n Tic-Tac-Toe Game - Databricks (medium)
  • Optimal Commute: Nearest Transit Distance in a City Grid - Databricks (medium)
|Home/Coding & Algorithms/Databricks

Implement Generalized Tic-Tac-Toe

Databricks logo
Databricks
Jul 7, 2026, 12:00 AM
hardSoftware EngineerOnsiteCoding & Algorithms
0
0

Implement Generalized Tic-Tac-Toe

Implement a TicTacToe class for two players on an m by n board with a configurable winning length k.

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?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Databricks•More Software Engineer•Databricks Software Engineer•Databricks Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ 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.