← Questions

Connect Four board with gravity drops and board snapshots

0
SnowflakeCoding

A coding interview question asked at Snowflake. It tests object-oriented design, matrix and simulation.

Build the board for a gravity-based four-in-a-row game. Players drop single-character colored pieces into columns. A piece falls straight down and stops in the lowest empty cell of its column.

Implement ConnectFourBoard:

  • ConnectFourBoard(rows, cols) creates an empty board with rows rows and cols columns. Every cell starts as the character '0'. Row 0 is the top row, and row rows - 1 is the bottom.
  • drop(color, col) -> List[List[str]] drops a piece of color into column col, then returns the board after the move as a 2D list of characters.
    • color is any single character other than '0', for example 'R', 'Y' or 'G'.
    • If column col is already full, the board does not change, and you still return its current state.
    • The returned grid must be an independent copy. Changes the caller makes to it must not affect the board, and later drops must not change a grid returned earlier.
    • Raise an error if col is outside 0..cols-1.

Part 2. Add last_move_wins() -> bool. It returns true when the piece placed by the most recent successful drop forms a line of at least 4 pieces of its color: horizontal, vertical or diagonal. It returns false if the last drop hit a full column.

Constraints. 1 <= rows, cols <= 100. There are at most 10^4 calls to drop.

Example 1

b = ConnectFourBoard(3, 3)
b.drop('G', 2)  ->  [['0','0','0'],
                     ['0','0','0'],
                     ['0','0','G']]
b.drop('B', 2)  ->  [['0','0','0'],
                     ['0','0','B'],
                     ['0','0','G']]

Example 2

b = ConnectFourBoard(2, 2)
b.drop('G', 0)
b.drop('B', 0)
b.drop('G', 0)  ->  [['B','0'],
                     ['G','0']]   # column 0 is full, so nothing changes

Clarifying questions

  • Should an invalid color ('0' or more than one character) raise?
  • After a drop into a full column, does last_move_wins() look at that failed drop or the last successful one? (The statement says false: confirm.)
  • Should last_move_wins() be false before any drop?
  • Is copying the whole board on every drop acceptable, given at most 10^4 drops on up to 100 x 100?

What a strong answer covers

  • Keeps the grid plus the next free row per column, so a drop is O(1) before the copy.
  • Returns a fresh copy ([row[:] for row in grid]) so neither side can change the other; notes the O(rows * cols) cost per call.
  • Records the last placed cell (or None after a failed drop) and checks 4 directions from it, counting both ways, O(1) for a line of 4.
  • Raises on an out-of-range column, and leaves the board unchanged on a full column.
  • Tests the aliasing rule directly (mutate a returned grid, then drop), a full column, and wins along both diagonals near the edges.

Follow-ups

  • Make snapshots cheaper (copy-on-write or persistent rows) when the board is large.
  • Generalise the line length to k and report the winning cells.
  • Add turn order and a full game engine on top of the board.

Where it's asked

What it tests

object-oriented design · matrix · simulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding