Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Tic-Tac-Toe OOP Design
00:00
5 left

Tic-Tac-Toe OOP Design

MediumPython

Problem

Wise runs its Software Engineer pair-programming round on a deliberately small problem so the interviewer can watch how you structure code, handle invalid input, and extend a design when the rules change. The classic version is Tic-Tac-Toe; the follow-ups make the board n x n and the winning run k in a row.

Implement play_tic_tac_toe(n, k, moves), which replays a list of moves on an empty n x n board and returns the state of the game.

Rules

  • X always moves first and the two players alternate. A move is [row, col], zero-indexed.
  • A player wins by placing k of their marks consecutively in a row, a column, or either diagonal direction, anywhere on the board.
  • The game ends the moment a player wins. Any move after that is invalid.
  • Moves are applied in order and validated one at a time. At the first invalid move, stop and return its reason. Do not apply any later moves.

Return value

Return exactly one of these strings:

  • "X wins" or "O wins" if a player has won.
  • "Draw" if every cell is filled and nobody has won.
  • "In progress" otherwise, including when moves is empty.
  • "Invalid move: out of bounds" if a move lies outside the board.
  • "Invalid move: cell occupied" if the target cell already holds a mark.
  • "Invalid move: game over" if a move is made after the game has been won.

Design the solution so that the board size and winning length are parameters, not constants, and so that the win check does not rescan the whole board on every move.

Function Signature

def play_tic_tac_toe(n, k, moves):

Constraints

  • 1 <= k <= n <= 100
  • 0 <= len(moves) <= 10^4
  • Each move is a pair of integers; values may be negative or exceed n - 1

Function Signature

def play_tic_tac_toe(n, k, moves):
Interviewer

Your question is Tic-Tac-Toe OOP Design. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.