Stage 1 of 3: The game
Two players, X and O, take turns marking the cells of a square board; whoever fills a whole line wins.
Implement Game(n), Game.play(row, col) and Game.render() in game.py.
-
Game(n)starts an empty board ofnrows andncolumns,nfrom 1 to 20. Rows and columns are numbered from 0, row 0 at the top. X moves first, and the players alternate after every accepted move. -
play(row, col)puts the mark of the player to move on that cell and returns:"X wins"or"O wins"when that player now holds every cell of a row, of a column, of the diagonal from(0, 0)to(n - 1, n - 1), or of the diagonal from(0, n - 1)to(n - 1, 0);- otherwise
"draw"when every cell is marked; - otherwise
"ongoing".
A win or a draw ends the game.
-
playreturns"invalid"and changes nothing, so the same player is still to move, when the cell is off the board (roworcolbelow 0 or aboven - 1), when the cell already holds a mark, or when the game has ended. -
render()returns the board as text: one line per row, top row first, one character per cell (X,O, or.for an empty cell), lines joined by"\n"with no newline at the end. It can be called at any time, after the end too.
Each Game is a game of its own; several can be played at once.
Example
game = Game(3)
game.play(1, 1) # "ongoing"
game.play(0, 0) # "ongoing"
game.play(2, 0) # "ongoing"
game.play(0, 1) # "ongoing"
game.play(0, 2) # "X wins": X holds (0, 2), (1, 1) and (2, 0)
game.play(2, 2) # "invalid": the game is over
game.render() # "OOX\n.X.\nX.."
Notes
- Moves are always given as integers.
- On the starter, all 13 tests in
tests/test_part1.pyfail.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
game.py - Read-only: all
tests/test_*.pyfiles - Rows and columns are numbered from 0, row 0 at the top; moves are always given as integers
What comes after stage 1
The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: k in a row, any size. Any rectangle, any run length, and boards up to 10^15 cells a side.
- Stage 3: A computer player. A computer player takes a random empty cell, on boards just as large.
The files you get
- Yours to edit: game.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: an agent in the editor
The assistant here works like the one in the interview IDE. It reads your files, runs code and proposes changes, and you accept or reject each one. It doesn't volunteer ideas, so what gets built is what you asked for. Along the way the interviewer asks about your decisions, and at the end the report says what you checked and what you accepted unread.
Frequently asked, briefly answered
Is Tic-Tac-Toe free?
It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.
Which company asks something like this?
Databricks. Based on candidates' reports of Databricks interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The assistant is in the editor, same as in the interview IDEs at Meta, Stripe and Amazon. What you choose at Begin is the format: an agent that proposes changes you accept or reject, or an answer-only assistant that can't write code.
What does the interviewer ask?
6 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.
More like it: every practice problem, how Databricks runs its AI round, the AI coding interview guide.