KatInterview

Square Grid Line-Completion Referee

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designdesignsimulationcounting

Last 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 as operations. The entry at index i is [row, col, player], describing the i-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:

  • 0 if that move did not complete a line for anyone,
  • otherwise the number of the player who made the move (1 or 2).

Rules

  • Every move targets a cell that is currently empty, so no cell is marked twice.
  • A line is won only if all n of 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 a ValueError.

Constraints

  • 2 <= n <= 1000
  • player is 1 or 2
  • 0 <= row, col < n
  • len(operations) == len(arguments)
  • Moves are always made on empty cells.
  • Aim for O(1) time per move and O(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.