Home
← All questions

Connect 4 game | Frontend Machine Coding

MediumPremiumMachine Coding

Frontend Machine Coding interview practice: Build a 6x7 Connect Four with column drops, alternating players, and win detection.

Problem

Create a Connect 4 game on the client with changes reflected on the UI.

Requirements

  • Build a 6 (rows) x 7 (cols) board.
  • Two players, Blue and Red, alternate turns; one move per turn.
  • The player selects a column (e.g. a button per column); the disc falls to the lowest empty row in that column.
  • Win condition: 4 consecutive same-color discs horizontally, vertically, or diagonally.
  • UI must update so a user can interact and see the board change.
+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |   <- column selectors
+---+---+---+---+---+---+---+
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . |
| . | R | . | B | . | . | . |
| B | R | B | R | . | . | . |
+---+---+---+---+---+---+---+   B = Blue, R = Red, . = Empty

Hints / Approach

  • Board as 6 x 7 2D array. Dropping in column c: scan from the bottom row up for the first empty cell.
  • Win check from the last placed disc (r, c): count consecutive same color in all 4 directions (both ways) and check if total >= 4.
  • Track currentPlayer, winner, and disable input after a win or full board (draw).
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium