Tic-Tac-Toe Game Logic With a Random-Move Computer Player

Read the full interview experience this question came from →

Quick Overview

A coding round on tic-tac-toe game logic: model the board, validate moves, track turns and detect wins and draws, then add a computer opponent that plays random legal moves. It tests clean object design, separating the rules from input and output, and keeping randomness testable.

Tic-Tac-Toe Game Logic With a Random-Move Computer Player

Company: Databricks

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

Implement the logic of a tic-tac-toe game. The focus is the game logic, not a user interface. Two players, X and O, take turns placing their mark on an empty cell of the board; the first player to fill a whole row, column or diagonal with their mark wins, and a full board with no winner is a draw. Late in the round a second part was added: let the computer play one side, choosing its moves at random. ### Clarifying Questions - Is the board always 3 × 3, or should it support an n × n board where a full row, column or diagonal wins? - Who moves first, and is the human always X? - How should an invalid move be reported: an exception, an error value, or a prompt to try again? - Is the game driven by a console loop, or by method calls from tests or another program? ### Part 1 — Game logic Write the game state and the operations two players need: make a move, reject invalid moves, detect a win or a draw after each move, and report whose turn it is and whether the game is over. ```hint Only the last move can finish a line A move can only complete a line that passes through the cell just played. Think about how to test that without rescanning the whole board. ``` #### What This Part Should Cover - A clear state model: the board, the player to move, and the game status - Validation of every move: bounds, occupied cells, and moves after the game has ended - Correct win and draw detection, and its cost per move ### Part 2 — Random computer player Add a computer player that chooses uniformly at random among the empty cells, and run a game between a human and the computer, or between two computer players. ```hint Keep the randomness injectable Decide how a test can make the computer's moves predictable without changing the game code. ``` #### What This Part Should Cover - A uniform choice among legal moves only - A player abstraction that lets human and computer players share one game loop - Deterministic tests through a seeded or injected random source ### What a Strong Answer Covers - Settles board size, turn order and error handling before coding - Separates the rules and state from input and output and from player strategy - Validates every move and ends the game cleanly on a win or a draw - Tests for wins on rows, columns and both diagonals, for draws, and for invalid moves - Talks through the code while writing it and names improvements beyond the minimum, such as a stronger computer player ### Follow-up Questions - Generalize to an n × n board where k marks in a row win. What changes in win detection? - Make the computer take a winning move when it has one and block the opponent's immediate win otherwise. How do you add this without touching the game rules? - How would you make the computer never lose on a 3 × 3 board, and what does that cost? - How would you add undo?

Overview: A coding round on tic-tac-toe game logic: model the board, validate moves, track turns and detect wins and draws, then add a computer opponent that plays random legal moves. It tests clean object design, separating the rules from input and output, and keeping randomness testable.

Read the full Databricks Software Engineer interview experience this question came from

Databricks logo
Databricks
Oct 14, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
1
0

Implement the logic of a tic-tac-toe game. The focus is the game logic, not a user interface. Two players, X and O, take turns placing their mark on an empty cell of the board; the first player to fill a whole row, column or diagonal with their mark wins, and a full board with no winner is a draw. Late in the round a second part was added: let the computer play one side, choosing its moves at random.

Clarifying Questions Guidance

  • Is the board always 3 × 3, or should it support an n × n board where a full row, column or diagonal wins?
  • Who moves first, and is the human always X?
  • How should an invalid move be reported: an exception, an error value, or a prompt to try again?
  • Is the game driven by a console loop, or by method calls from tests or another program?

Part 1 — Game logic

Write the game state and the operations two players need: make a move, reject invalid moves, detect a win or a draw after each move, and report whose turn it is and whether the game is over.

What This Part Should Cover Guidance

  • A clear state model: the board, the player to move, and the game status
  • Validation of every move: bounds, occupied cells, and moves after the game has ended
  • Correct win and draw detection, and its cost per move

Part 2 — Random computer player

Add a computer player that chooses uniformly at random among the empty cells, and run a game between a human and the computer, or between two computer players.

What This Part Should Cover Guidance

  • A uniform choice among legal moves only
  • A player abstraction that lets human and computer players share one game loop
  • Deterministic tests through a seeded or injected random source

What a Strong Answer Covers Guidance

  • Settles board size, turn order and error handling before coding
  • Separates the rules and state from input and output and from player strategy
  • Validates every move and ends the game cleanly on a win or a draw
  • Tests for wins on rows, columns and both diagonals, for draws, and for invalid moves
  • Talks through the code while writing it and names improvements beyond the minimum, such as a stronger computer player

Follow-up Questions Guidance

  • Generalize to an n × n board where k marks in a row win. What changes in win detection?
  • Make the computer take a winning move when it has one and block the opponent's immediate win otherwise. How do you add this without touching the game rules?
  • How would you make the computer never lose on a 3 × 3 board, and what does that cost?
  • How would you add undo?