KatInterview

Board Game: Detect k-in-a-Row on a Rectangular Grid

Data Structure DesignAuto-graded · 24 test cases
MediumAuto-gradedData Structure Designdesignarrayssimulation

Last updated

Tests incremental win detection on an m×n board by tracking consecutive run lengths at endpoints to check for k-in-a-row in O(1) per move.

You are building the core logic for a two-player grid game on an m × n board. A player wins by placing k of their marks in an unbroken line through any newly placed piece.

Given the board dimensions, the win length, and a sequence of moves, return the result of each move: the winning player's id if that move ends the game, or 0 if the game is still ongoing after that move.

The Game

The board has m rows and n columns, with positions indexed from (0, 0) at the top-left. Two players, identified as 1 and 2, take turns placing marks. A move is a triple (row, col, player). Players are not guaranteed to alternate turns.

A move is a winning move if, once the mark is placed, that player has at least k of their own marks in an unbroken sequence passing through the newly placed cell. The sequence may run horizontally, vertically, along the main diagonal (top-left to bottom-right), or along the anti-diagonal (top-right to bottom-left). A run of length strictly greater than k still counts as a win.

Input

  • m — number of rows
  • n — number of columns
  • k — number of consecutive marks needed to win
  • moves — a list of (row, col, player) triples

Output

A list of integers with exactly one entry per move, in order. Each entry is player if that move wins the game, or 0 otherwise. No move is ever given after a winning move, so the game ends either with a win or with the board partially filled and no winner.

Constraints

  • 1 <= m, n <= 1000
  • 1 <= k <= max(m, n)
  • 1 <= len(moves) <= min(m * n, 10^5)
  • Every move is valid: the cell is within bounds, unoccupied, and player is 1 or 2
  • No move is provided after a winning move

Examples

Example 1

Input: play_grid_game(3, 3, 3, [[0, 0, 1], [0, 2, 2], [2, 2, 1], [1, 1, 2], [2, 0, 1], [1, 0, 2], [2, 1, 1]])

Output: [0, 0, 0, 0, 0, 0, 1]

Example 2

Input: play_grid_game(4, 5, 3, [[0, 4, 2], [3, 0, 1], [1, 3, 2], [0, 0, 1], [2, 2, 2]])

Output: [0, 0, 0, 0, 2]

Example 3

Input: play_grid_game(2, 4, 4, [[0, 0, 1], [1, 0, 2], [0, 1, 1], [1, 1, 2], [0, 2, 1], [1, 2, 2], [0, 3, 1]])

Output: [0, 0, 0, 0, 0, 0, 1]

Hints

Hint 1

Start with the simpler O(k) approach: after placing a mark, scan outward in each of the 4 directions up to k−1 steps on each side and count how far the same player's marks extend. This is correct and worth coding first before optimizing.

Hint 2

To reach O(1) per move, think about what information you'd need already stored so you don't have to scan. For a run of consecutive marks, which cells actually need to hold the run's length? You don't need every cell to be accurate — only the ones you'll read next time.

Hint 3

The key insight: store the run length at both endpoints of every maximal run. When you place a new mark, its immediate same-player neighbors (if any) must be endpoints of their respective runs, because the cell you just filled was empty a moment ago. Read those stored lengths, add them together with 1 for the new mark, and write the merged length to the new endpoints.