Match-3 Board Simulation
Asked of: Software Engineer
Last updated

What's being tested
Match-3 board simulation tests grid scanning, repeated state mutation, and termination when no more eliminations exist. Interviewers are probing whether you can separate detection from deletion, apply gravity correctly, and reason about complexity over multiple cascades.
Patterns & templates
-
Two-phase update: first mark all crushable cells, then mutate; deleting during scan causes missed overlapping horizontal/vertical matches.
-
Row/column run detection: scan consecutive equal nonzero values with
whileloops; mark runs wherelength >= 3. -
Marker encoding: use negative values or a separate
to_crushboolean grid; preserve original candy type while detecting overlapping groups. -
Gravity compaction: for each column, write non-crushed values from bottom to top using
write_row, then fill remaining cells with0. -
Fixed-point iteration: repeat
detect -> crush -> gravityuntil no cells are marked; worst-case time isO(kmn)forkcascades. -
Connected group variant: if removal is by connected components instead of straight runs, use BFS/DFS with
visitedand size threshold checks. -
Clean function split: implement
find_matches(board),apply_gravity(board), andresolve(board)to simplify debugging and interviewer follow-ups.
Common pitfalls
Pitfall: Mutating cells to
0during detection can break detection of crossing matches, such as a horizontal and vertical run sharing one cell.
Pitfall: Gravity must preserve vertical order within each column; sorting or rebuilding incorrectly changes relative candy positions.
Pitfall: Forgetting the loop termination condition leads to returning after one crush instead of resolving all cascades.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
Related concepts
- Connect Four And Grid Game EnginesCoding & Algorithms
- Grid, Matrix And Spatial AlgorithmsCoding & Algorithms
- Dynamic Programming, Backtracking, And Combinatorial SearchCoding & Algorithms
- Graph Search, State Space, And Path OptimizationCoding & Algorithms
- Dynamic Programming, Backtracking, and State-Space SearchCoding & Algorithms
- Arrays, Sliding Windows, DP And Stack PatternsCoding & Algorithms