PythonStudio
Interview Problems

Tic-Tac-Toe

easy · game state, turn validation

Brief-reported interview attribution (unverified): Microsoft, Amazon

How to approach an LLD round

Implement a 3×3 Tic-Tac-Toe game. X moves first. move returns "ok" for a legal move, "invalid" for an illegal one, "X wins" / "O wins" when a move completes a row, column, or diagonal, and "draw" when the board is full with no winner.

class TicTacToe:
    def __init__(self):
    def move(self, player, row, col):
        # player: "X" or "O"; row, col: 0-2
        # -> "ok" | "invalid" | "X wins" | "O wins" | "draw"
    def board(self):
        # -> 3x3 list of lists, "" for an empty cell
    def current_player(self):
        # -> "X" or "O"
    def status(self):
        # -> "in progress" | "X wins" | "O wins" | "draw"

Rules. An illegal move (wrong player's turn, out of bounds, occupied cell, or any move after the game is over) returns "invalid" and does not change the turn or the board. A win is checked after every legal move; a draw is checked only when the board is full and nobody has won.

Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.

Public demo

# Public demo: run after implementing the class API above.
g = TicTacToe()
print(g.move('O', 0, 0))
print(g.move('X', 3, 0))

Starter code

class TicTacToe:

    def __init__(self):
        """Initialize the state described in the problem."""
        pass

    def current_player(self):
        '''-> "X" or "O"'''
        pass

    def status(self):
        '''-> "in progress" | "X wins" | "O wins" | "draw"'''
        pass

    def board(self):
        """-> 3x3 list of lists, "" for an empty cell"""
        pass

    def move(self, player, row, col):
        '''player: "X" or "O"; row, col: 0-2 -> "ok" | "invalid" | "X wins" | "O wins" | "draw"'''
        pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler