Connect Four And Grid Game Engines
Asked of: Software Engineer
Last updated

What's being tested
Grid game engines test clean state modeling, turn validation, and efficient winner detection after each move. The core pattern is checking only lines crossing the last placed token, not rescanning the whole board.
Patterns & templates
-
Board representation — use
grid[rows][cols]plusheights[col];drop(col, player)becomesO(1)placement with bounds checks. -
Last-move win check — after
(r, c), scan four direction pairs: horizontal, vertical, diagonal down, diagonal up. -
Connect-N counting —
count(dir) + count(opposite) + 1 >= n; each move costsO(n)orO(rows + cols)worst case. -
API design — expose
move(col),getWinner(),isDraw(),currentPlayer; reject moves after win or into full columns. -
Generalization template — parameterize
rows,cols, andtarget; validate impossible configs liketarget > max(rows, cols)carefully. -
Testing strategy — cover vertical, horizontal, both diagonals, full-column rejection, draw state, invalid player turns, and win-on-last-move cases.
Common pitfalls
Pitfall: Rescanning the entire board after every move is simple but often misses the intended optimization; anchor detection on the last token.
Pitfall: Diagonal logic is error-prone; define direction vectors once, such as
(1,1)and(1,-1), then reuse symmetric counting.
Pitfall: Forgetting terminal-state behavior leads to bugs where players keep moving after a win or overwrite draw/winner state.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Design a Connect-Four-like board gameAirbnb · Software Engineer · Onsite · hard
- Implement Connect Four gameAirbnb · Software Engineer · Technical Screen · Medium
- Implement Connect Four with win detectionAirbnb · Software Engineer · Take-home Project · Medium
- Design Connect-N winner detectorAirbnb · Software Engineer · Technical Screen · Medium
- Design and implement Connect Four engineAirbnb · Software Engineer · Technical Screen · Medium
Related concepts
- Match-3 Board SimulationCoding & Algorithms
- Graphs, Grids, And Connected ComponentsCoding & Algorithms
- Grid, Matrix And Spatial AlgorithmsCoding & Algorithms
- Graph Search, State Space, And Path OptimizationCoding & Algorithms
- Graph Search, Pathfinding, And ConnectivityCoding & Algorithms
- Graph, Grid, BFS/DFS, And Union-FindCoding & Algorithms