Implement Connect Four Winner Detection
Virtu FinancialData Scientist · Technical ScreenLast updated
This question evaluates the ability to design and implement game-state logic for a Connect Four-style board, covering board data structures, coordinate and boundary handling, gravity-based token placement, and efficient pattern detection for consecutive tokens in the Coding & Algorithms domain.
Implement a Connect Four-style game engine.
The board has 6 rows and 7 columns. Two players alternate turns. On each turn, the current player chooses a column. Because of gravity, the player's token falls to the lowest empty cell in that column. A move is invalid if the chosen column is already full.
After each valid move, determine whether the current player has won. A player wins if they have four of their own tokens consecutively in any of the following directions:
- Horizontal
- Vertical
- Diagonal from bottom-left to top-right
- Diagonal from top-left to bottom-right
Design and implement the core logic for placing a token and checking whether the latest move creates a win.
Follow-up questions:
- How would you support more than two players?
- How would you optimize the solution if the board were very large?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.