This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
Carefully perform the "crush" and "gravity" steps. In the crush step, flag each candy that should be removed, then go through and crush each flagged candy. In the gravity step, collect the candy in each column and then rewrite the column appropriately. Do these steps repeatedly until there's no work left to do.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, simulation-based grid problems like Candy Crush are occasionally used in technical interviews. They test matrix traversal, careful state updates, and the ability to model real-world processes algorithmically.
A 2D array or matrix is the most suitable data structure since the board represents a grid. It allows efficient traversal for detecting horizontal and vertical matches and supports in-place updates when candies are crushed and dropped.
The optimal approach is a simulation technique. You repeatedly scan the grid to mark sequences of three or more identical candies, then apply gravity so remaining candies fall downward. This process continues until no more candies can be crushed.
Gravity is simulated column by column. Using a pointer from the bottom of each column, you move non-crushed candies downward and fill the remaining cells above with zeros. This mimics candies falling after matches disappear.