Dot grid game winner
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Google. It tests simulation, grid and game.
Two players play a dots-and-boxes style game on a 9 x 9 grid of dots. The grid has 8 x 8 = 64 unit squares ("boxes"). Rows and columns are numbered 0 to 8.
You are given moves, a list of the sticks placed in order. moves[i] = [r1, c1, r2, c2] places a stick between dot (r1, c1) and dot (r2, c2). The two dots are always horizontally or vertically adjacent, and they may appear in either order.
Players strictly alternate. Player 1 makes moves 1, 3, 5, ... and player 2 makes moves 2, 4, 6, ..., whether or not a move closes a box. Whoever places the fourth side of a box scores that box. A single stick can close two boxes at once, and both count for its player.
Implement find_winner(moves: list[list[int]]) -> int. Return 1 or 2 for the player who owns more boxes after all moves, or 0 for a tie.
Constraints
-
0 <= len(moves) <= 144(the total number of sticks on a 9 x 9 dot grid) 0 <= r1, c1, r2, c2 <= 8- No stick is placed twice. (A natural follow-up asks you to validate this yourself.)
Example 1
moves = [[0,0,0,1], [0,0,1,0], [1,0,1,1], [0,1,1,1]]
output: 2
The first four sticks are the four sides of the top-left box. The 4th move is player 2's, so player 2 scores 1 and player 1 scores 0.
Example 2
moves = [[0,1,0,0], [5,5,5,6], [1,0,0,0], [6,6,6,7], [1,1,1,0], [7,7,7,8], [1,1,0,1]]
output: 1
Player 1's sticks (moves 1, 3, 5, 7) are the four sides of the top-left box, written with the endpoints reversed. Player 1 places the last side on move 7. Player 2's sticks close nothing.
Expected: O(1) work per move.
Clarifying questions
- Are the sticks guaranteed to be in bounds and between adjacent dots, or should invalid sticks be rejected?
- Turns alternate even when a move closes a box, unlike the usual dots-and-boxes rule. Is that right?
- Should the function also return the scores, or only the winner?
What a strong answer covers
- Normalizes each stick (orders its endpoints), tells horizontal from vertical, and keeps a side count for each of the 64 boxes.
- A horizontal stick from
(r, c)to(r, c + 1)borders boxes(r - 1, c)and(r, c); a vertical stick from(r, c)to(r + 1, c)borders(r, c - 1)and(r, c). Boxes outside the grid are skipped. - Adds one to each bordering box; a box that reaches 4 goes to the player of the current move, found from the move's index. O(1) per move.
- Handles a stick that closes two boxes at once, endpoints in either order (Example 2), sticks on the outer edge, and an empty move list (a tie, so 0).
- Tests the two examples, a double close, and an outer-edge stick.
Follow-ups
- Validate the input: repeated sticks, dots that are not adjacent, coordinates out of range.
- The real rule: a player who closes a box moves again.
- Any
R x Cgrid, with the final score per player. - From a position, find a move that closes a box, or the best move by searching the game tree.
Where it's asked
What it tests
simulation · grid · game
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
