Square Grid Line-Completion Referee
Data Structure DesignAuto-graded · 14 test casesLast updated
Process a batch of moves on an n x n two-player grid and report after each move whether the mover has completed a full row, column, or diagonal, using constant-time per-move bookkeeping.
Square Grid Line-Completion Referee
Two players take turns marking cells of an n x n grid. Player 1 and player 2 each place their own mark. A player wins as soon as they own every cell of one full line, where a line is any of:
- a whole row,
- a whole column,
- the main diagonal (cells where
row == col), - the anti-diagonal (cells where
row + col == n - 1).
You are given the whole sequence of operations as a batch. Implement a function that processes them in order and reports the outcome after each one.
Input
n: the side length of the grid.operations: a list of strings, one per operation. Every entry is"move".arguments: a list of the same length asoperations. The entry at indexiis[row, col, player], describing thei-th move:player(1 or 2) marks the cell at 0-indexed position(row, col).
Output
Return a list of integers with one entry per operation, in order:
0if that move did not complete a line for anyone,- otherwise the number of the player who made the move (
1or2).
Rules
- Every move targets a cell that is currently empty, so no cell is marked twice.
- A line is won only if all
nof its cells belong to the same player. A line containing marks from both players can never be won. - The cell at the center of an odd-sized grid lies on both diagonals, so a move there counts toward both.
- For each move, report a win if, after applying that move, any row, column, or diagonal is fully owned by a single player. Your solution should do a constant amount of work per move (not scan the line).
- Treat the reported result exactly as specified above: after applying each move, a win is reported whenever the row of the move, the column of the move, the main diagonal, or the anti-diagonal is fully owned by one player, and the reported winner is the player who just moved.
- If an operation is anything other than
"move", raise aValueError.
Constraints
2 <= n <= 1000playeris1or20 <= row, col < nlen(operations) == len(arguments)- Moves are always made on empty cells.
- Aim for
O(1)time per move andO(n)extra space.
Examples
Example 1
Input: line_completion_referee(3, ['move', 'move', 'move', 'move', 'move'], [[0, 0, 1], [0, 1, 2], [1, 1, 1], [0, 2, 2], [2, 2, 1]])
Output: [0, 0, 0, 0, 1]
Hints
Hint 1
You do not need to remember the whole board. Think about what you must know about each line to decide whether one player owns all of it.
Hint 2
Give player 1 a score of +1 and player 2 a score of -1, and keep a running total per row, per column, and for each of the two diagonals. When can a total have absolute value n?
Hint 3
A cell can sit on both diagonals (the center of an odd grid), so update the two diagonal totals with two independent checks, not an if/else.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Storing the grid and scanning lines after every move costs O(n) per move. Instead, observe that a line is won only when all n of its cells belong to one player, so we only need a per-line tally, not the board.
Give player 1 a mark of +1 and player 2 a mark of -1. Maintain:
- one running sum per row,
- one running sum per column,
- one sum for the main diagonal (
row == col), - one sum for the anti-diagonal (
row + col == n - 1).
For a move at (row, col), add the mark to rows[row] and cols[col], and to each diagonal that contains the cell (two independent if checks, since the center of an odd grid is on both). Then, if any of the four relevant sums has absolute value n, the mover wins; otherwise the result is 0.
Why it works
- Every line has exactly
ncells, and each filled cell contributes exactly+1or-1once (moves go on empty cells). So a line's sum has absolute value at mostn, and equalsnin absolute value only if allncontributions share a sign, meaning one player owns the entire line. - A line with marks from both players has a sum strictly between
-nandn, so it is never reported. - A line can only reach absolute value
non the move that fills its last cell, so the winner is the player who just moved. - Both diagonal sums are checked on every move, which matches the specified behavior.
Complexity
- Time:
O(1)per move,O(m)formoperations. - Space:
O(n)for the row and column sums plus two scalars.
Reference solution
from typing import List
def line_completion_referee(n: int, operations: List[str], arguments: List[List[int]]) -> List[int]:
rows = [0] * n
cols = [0] * n
diagonal = 0
anti_diagonal = 0
results = []
for operation, args in zip(operations, arguments):
if operation != "move":
raise ValueError("Unsupported operation")
row, col, player = args
mark = 1 if player == 1 else -1
rows[row] += mark
cols[col] += mark
if row == col:
diagonal += mark
if row + col == n - 1:
anti_diagonal += mark
if (abs(rows[row]) == n or abs(cols[col]) == n or
abs(diagonal) == n or abs(anti_diagonal) == n):
results.append(player)
else:
results.append(0)
return results