Design a Connect 4 game backend with win and draw detection

Quick Overview

Design the backend for a 6 by 7 Connect 4 game that drops pieces into columns, rejects invalid moves, and reports a win in any of four directions or a draw after each move. It tests class design, game state tracking, move validation order, and an efficient win check around the last piece.

Design a Connect 4 game backend with win and draw detection

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a backend for the classic Connect 4 game played on a `6` row by `7` column grid. Two players take alternating turns, with player `1` moving first. On each turn, the current player chooses a column to drop their piece into. The piece falls to the lowest empty row of that column. After every move, the system must detect whether the move produced a winning line of `4` consecutive pieces in any of the four directions: horizontal, vertical, diagonal down-right, or diagonal down-left. The class maintains all game state internally across calls, so callers only need to invoke `dropPiece` with a column index to play a full game. Implement the following methods: - `dropPiece(column: int) -> int`: drop the current player's piece into the given column. Returns `0` if the move was made with no winner yet, `1` or `2` if that player just won, `3` if the move filled the board producing a draw, or `-1` if the move is invalid (column out of range, column full, or the game is already over). - `getCurrentPlayer() -> int`: return the player whose turn it is (`1` or `2`). Returns `0` if the game has ended. - `getWinner() -> int`: return `1` or `2` if that player has won, `3` for a draw, or `0` if the game is still in progress. - `getBoard() -> List[List[int]]`: return a snapshot of the board as a 2D list with `6` rows and `7` columns. Row index `0` is the bottom row. `0` denotes empty, and `1` and `2` denote pieces from the respective players. Example 1: ```text Input: ["Connect4", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "getWinner"] [[], [0], [1], [0], [1], [0], [1], [0], []] Output: [null, 0, 0, 0, 0, 0, 0, 1, 1] game = Connect4() # empty 6x7 board, player 1 to move game.dropPiece(0) # -> 0 (P1 at col 0, row 0) game.dropPiece(1) # -> 0 (P2 at col 1, row 0) game.dropPiece(0) # -> 0 (P1 at col 0, row 1) game.dropPiece(1) # -> 0 (P2 at col 1, row 1) game.dropPiece(0) # -> 0 (P1 at col 0, row 2) game.dropPiece(1) # -> 0 (P2 at col 1, row 2) game.dropPiece(0) # -> 1 (P1 at col 0, row 3: four in a column, P1 wins) game.getWinner() # -> 1 (player 1 has won) ``` Example 2: ```text Input: ["Connect4", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "getCurrentPlayer"] [[], [3], [3], [2], [4], [1], [5], [0], [7], []] Output: [null, 0, 0, 0, 0, 0, 0, 1, -1, 0] game = Connect4() # empty board, player 1 starts game.dropPiece(3) # -> 0 (P1 at col 3, row 0) game.dropPiece(3) # -> 0 (P2 at col 3, row 1, on top of P1) game.dropPiece(2) # -> 0 (P1 at col 2, row 0) game.dropPiece(4) # -> 0 (P2 at col 4, row 0) game.dropPiece(1) # -> 0 (P1 at col 1, row 0) game.dropPiece(5) # -> 0 (P2 at col 5, row 0) game.dropPiece(0) # -> 1 (P1 fills cols 0 to 3 on row 0, P1 wins) game.dropPiece(7) # -> -1 (game is over, and column 7 is out of range) game.getCurrentPlayer() # -> 0 (game has ended) ``` ```hint Which lines can change After a move, only a few lines on the board can have changed. Limit the win check to those, and remember that the new piece can sit in the middle of a line, with matching pieces on both sides. ``` ```hint Order of checks Decide the order in which `dropPiece` rejects a move, places the piece and reports a result. Think about the move that fills the last empty cell: what should it return if it also completes a line of four? ``` ### Constraints & Assumptions - Board dimensions: `6` rows by `7` columns. - Two players identified by `1` and `2`; player `1` moves first. - `dropPiece` receives an integer column. Valid columns are `0` to `6`; any other value is an invalid move and returns `-1`. - A win requires `4` consecutive pieces of the same player horizontally, vertically, or along either diagonal. - Once the game is decided (win or draw), further `dropPiece` calls must return `-1`. ### Clarifying Questions to Ask - Does an invalid move (full or out-of-range column) leave the turn with the same player and the board unchanged? - Should `getBoard` return a copy, or is it acceptable for callers to see the live internal grid? - Are the board size and the win length fixed at 6 by 7 and 4, or should they be configurable? - Does one object hold one game, or should the backend host many games at once and support starting a new one? ### What a Strong Answer Covers - The game state: board orientation with row `0` at the bottom, the next free row per column, the current player, the result, and a move count. - The validation order in `dropPiece` (game over, column range, full column) and the guarantee that a rejected move changes nothing. - A win check limited to the four lines through the last placed piece, counting in both directions with bounds checks. - Draw detection when the board fills, with a win on the final move taking precedence, and `getCurrentPlayer` and `getWinner` staying consistent after the game ends. - Per-call complexity and a defensive copy in `getBoard`. ### Follow-up Questions - How would you generalize the class to an R by C board with a win length of K, and what would the win check cost? - How would you add an `undo` for the last move? - How would you serve many games through an HTTP API, and how would you stop two requests from racing on the same game? - How would you add a computer opponent that picks a move?

Overview: Design the backend for a 6 by 7 Connect 4 game that drops pieces into columns, rejects invalid moves, and reports a win in any of four directions or a draw after each move. It tests class design, game state tracking, move validation order, and an efficient win check around the last piece.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a backend for the classic Connect 4 game played on a 6 row by 7 column grid. Two players take alternating turns, with player 1 moving first.

On each turn, the current player chooses a column to drop their piece into. The piece falls to the lowest empty row of that column. After every move, the system must detect whether the move produced a winning line of 4 consecutive pieces in any of the four directions: horizontal, vertical, diagonal down-right, or diagonal down-left.

The class maintains all game state internally across calls, so callers only need to invoke dropPiece with a column index to play a full game.

Implement the following methods:

  • dropPiece(column: int) -> int : drop the current player's piece into the given column. Returns 0 if the move was made with no winner yet, 1 or 2 if that player just won, 3 if the move filled the board producing a draw, or -1 if the move is invalid (column out of range, column full, or the game is already over).
  • getCurrentPlayer() -> int : return the player whose turn it is ( 1 or 2 ). Returns 0 if the game has ended.
  • getWinner() -> int : return 1 or 2 if that player has won, 3 for a draw, or 0 if the game is still in progress.
  • getBoard() -> List[List[int]] : return a snapshot of the board as a 2D list with 6 rows and 7 columns. Row index 0 is the bottom row. 0 denotes empty, and 1 and 2 denote pieces from the respective players.

Example 1:

Input:
["Connect4", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "getWinner"]
[[],         [0],         [1],         [0],         [1],         [0],         [1],         [0],         []]

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

game = Connect4()        # empty 6x7 board, player 1 to move
game.dropPiece(0)        # -> 0   (P1 at col 0, row 0)
game.dropPiece(1)        # -> 0   (P2 at col 1, row 0)
game.dropPiece(0)        # -> 0   (P1 at col 0, row 1)
game.dropPiece(1)        # -> 0   (P2 at col 1, row 1)
game.dropPiece(0)        # -> 0   (P1 at col 0, row 2)
game.dropPiece(1)        # -> 0   (P2 at col 1, row 2)
game.dropPiece(0)        # -> 1   (P1 at col 0, row 3: four in a column, P1 wins)
game.getWinner()         # -> 1   (player 1 has won)

Example 2:

Input:
["Connect4", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "dropPiece", "getCurrentPlayer"]
[[],         [3],         [3],         [2],         [4],         [1],         [5],         [0],         [7],         []]

Output: [null, 0, 0, 0, 0, 0, 0, 1, -1, 0]

game = Connect4()        # empty board, player 1 starts
game.dropPiece(3)        # -> 0   (P1 at col 3, row 0)
game.dropPiece(3)        # -> 0   (P2 at col 3, row 1, on top of P1)
game.dropPiece(2)        # -> 0   (P1 at col 2, row 0)
game.dropPiece(4)        # -> 0   (P2 at col 4, row 0)
game.dropPiece(1)        # -> 0   (P1 at col 1, row 0)
game.dropPiece(5)        # -> 0   (P2 at col 5, row 0)
game.dropPiece(0)        # -> 1   (P1 fills cols 0 to 3 on row 0, P1 wins)
game.dropPiece(7)        # -> -1  (game is over, and column 7 is out of range)
game.getCurrentPlayer()  # -> 0   (game has ended)

Constraints & Assumptions

  • Board dimensions: 6 rows by 7 columns.
  • Two players identified by 1 and 2 ; player 1 moves first.
  • dropPiece receives an integer column. Valid columns are 0 to 6 ; any other value is an invalid move and returns -1 .
  • A win requires 4 consecutive pieces of the same player horizontally, vertically, or along either diagonal.
  • Once the game is decided (win or draw), further dropPiece calls must return -1 .

Clarifying Questions to Ask Guidance

  • Does an invalid move (full or out-of-range column) leave the turn with the same player and the board unchanged?
  • Should getBoard return a copy, or is it acceptable for callers to see the live internal grid?
  • Are the board size and the win length fixed at 6 by 7 and 4, or should they be configurable?
  • Does one object hold one game, or should the backend host many games at once and support starting a new one?

What a Strong Answer Covers Guidance

  • The game state: board orientation with row 0 at the bottom, the next free row per column, the current player, the result, and a move count.
  • The validation order in dropPiece (game over, column range, full column) and the guarantee that a rejected move changes nothing.
  • A win check limited to the four lines through the last placed piece, counting in both directions with bounds checks.
  • Draw detection when the board fills, with a win on the final move taking precedence, and getCurrentPlayer and getWinner staying consistent after the game ends.
  • Per-call complexity and a defensive copy in getBoard .

Follow-up Questions Guidance

  • How would you generalize the class to an R by C board with a win length of K, and what would the win check cost?
  • How would you add an undo for the last move?
  • How would you serve many games through an HTTP API, and how would you stop two requests from racing on the same game?
  • How would you add a computer opponent that picks a move?