KatInterview

Build a Configurable Disc-Drop Game Engine

Data Structure DesignModel answer
MediumModel answerData Structure Designgame-engineapi-design2d-array

Last updated

Design and implement a gravity-based four-in-a-row game engine with a clean API, local win detection, and support for arbitrary board sizes and run lengths.

Disc-Drop Game Engine

A classic two-player game is played on a vertical grid. Players alternate dropping discs into columns; each disc falls to the lowest unoccupied cell in that column. The first player to form an unbroken line of k discs — horizontally, vertically, or diagonally — wins. If every cell is filled with no winner, the game is a draw.

Your task is to implement a reusable engine for this game.


Default configuration

  • Board: 6 rows × 7 columns
  • Two players, identified as 1 and 2. Player 1 moves first.
  • Winning run length: k = 4
  • Row 0 is the bottom of the board; row = rows - 1 is the top.

The constructor should accept rows, cols, and k as optional parameters so the engine works on any valid board size.


Required API

drop(col, player) → int

Places the current player's disc in column col. Returns the row index where the disc lands.

Raises an InvalidMoveError (or equivalent) — without changing any state — for any of the following:

  • The game is already over (won or drawn).
  • player is not 1 or 2.
  • It is not player's turn.
  • col is out of range or not a valid integer.
  • The chosen column is already full.

get_status() → (str, player | None)

Returns the current game state as a tuple:

  • ("inProgress", None) — the game is still going
  • ("win", player) — that player has formed a run of k
  • ("draw", None) — the board is full with no winner

reset()

Restores the board to its initial empty state. Player 1 moves next.


Implementation requirements

Gravity in O(1). You must not scan a column to find the next empty cell. Instead, track the current fill height of each column directly.

Win detection in O(k). After each valid move, check only the four lines passing through the newly placed disc (horizontal, vertical, and both diagonals). Walk outward from the disc in both directions along each line, counting consecutive same-player cells. Do not scan the entire board.

Draw detection. Track the total number of moves made. A draw is declared when no win occurred and the board is completely filled. A move that simultaneously fills the last cell and forms a winning run must be reported as a win, not a draw.


Discussion points

Be ready to address:

  1. Why does local win detection (from the last move only) always produce a correct result? Argue from invariants.
  2. Compare a 2D array representation with bitboards: when does each approach pay off?
  3. What is the time and space complexity of each API method?
  4. What edge cases should unit tests cover? Write at least five meaningful test cases.

Constraints

  • 1 ≤ rows, cols ≤ 100
  • 1 ≤ k ≤ max(rows, cols)
  • A winning move on the very last empty cell counts as a win.
  • k larger than every dimension means no win is ever possible; the game will end in a draw if the board fills.

Hints

Hint 1

Track column fill heights in a separate array so the landing row for any drop is a direct lookup — no column scan needed. After placing the disc, increment the height for that column.

Hint 2

For win detection, walk outward from the newly placed cell along each of the four axis directions, counting consecutive same-player cells in both directions. The run through that cell is 1 + cells_forward + cells_backward. Cap each walk at k−1 steps so the total work stays O(k).

Hint 3

To distinguish a winning last move from a draw, always run the win check before checking whether moves == rows * cols. Only declare a draw if the board is full and no win was found.