MediumArrayTwo PointersMatrixSimulationUpdated Sep 2026

723. Candy Crush

Asked at Roblox

Difficulty

Medium

Time

O((m * n)^2)

Space

O(1)

Pattern

Array

Companies

1

Problem

You are given an m x n grid called board. Each cell holds an integer that names a candy type. The value 0 means the cell is empty.

The board is the state right after a player's move. Your job is to keep applying the game rules until nothing can change, then return the final board. One round works like this: first, find every group of three or more equal candies that sit in a straight horizontal or vertical line, and crush them all at the same time (those cells become 0). Then let candies fall: in each column, any candy with empty space below it drops straight down until it lands on another candy or on the bottom row. No new candy appears from above.

After a round, new groups may have formed, so you repeat the whole process. When a full round finds no group to crush, the board is stable — return it.

Crush, then drop, then repeat

board1355234331324522445514411The 5s in column 3 and the 4s in column 2 each form avertical run of three.
In one round all runs of three or more equal candies are removed at once, then the candies above fall down.

Constraints

  • ●m == board.length
  • ●n == board[i].length
  • ●3 <= m, n <= 50
  • ●1 <= board[i][j] <= 2000

Examples

Example 1
Inputboard = [[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]]
Output[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,114],[210,0,0,0,214],[310,0,0,113,314],[410,0,0,213,414],[610,211,112,313,614],[710,311,412,613,714],[810,411,512,713,1014]]
WhySeveral runs get crushed in the first rounds, which opens space and lets candies fall. The process keeps going until no line of three equal candies is left, so the top rows end up empty and the surviving candies settle at the bottom.
Example 2
Inputboard = [[1,3,5,5,2],[3,4,3,3,1],[3,2,4,5,2],[2,4,4,5,5],[1,4,4,1,1]]
Output[[1,3,0,0,0],[3,4,0,5,2],[3,2,0,3,1],[2,4,0,5,2],[1,4,3,1,1]]
WhyThe 5s in column 3 and the 4s in column 2 form vertical runs of three, so they are crushed together. The candies above them drop down, and after that no run of three remains, so this is the stable board.

Asked At

1 companies
CompanyDifficulty
RobloxMediumView all Roblox questions →

How to Think About It

1

All crushes in one round happen simultaneously. If you clear cells while scanning, you break runs that should also have been detected — so mark first, then clear.

2

Key trick for marking: negate the value of every candy that belongs to a run (board[r][c] = -abs(v)). Negative cells still compare equal by absolute value, so overlapping horizontal and vertical runs are all detected.

3

Gravity: process each column bottom-up with a write pointer. Copy every positive value down to the write pointer, then fill the remaining top cells with 0.

4

Repeat rounds until a round marks nothing.

5

Complexity per round is O(m * n); the number of rounds is bounded by how many candies can be crushed, so the total is O((m * n)²) in the worst case, which is fine for the 50x50 limit.

Optimal Approach

Step 1: Loop:
found = false.
Horizontal pass: for each r and c with c + 2 < n, let v = abs(board[r][c]); if v != 0 and the next two cells have the same absolute value, negate all three and set found = true.
Vertical pass: the same for r + 2 < m.
If not found, return the board.
Gravity: for each column, move positive values down with a write pointer and zero-fill the rest.

Time: O((m * n)²) worst case. Space: O(1) extra.

Mark, clear, then let gravity work

mark135-52343-31324-5224-4-5514-411Negating keeps the value for comparison, so horizontaland vertical runs can both be detected in one pass.after drop1300034052320312405214311Cleared cells become 0, and every candy above fallsstraight down to the lowest free spot.board[r][c] = -abs(v) to markrepeat until a round marks nothing
Negate every candy in a run so overlaps are found, then drop positive values down each column.
ComplexityTime O((m * n)^2)Space O(1)Each round scans the whole grid in O(m * n), and there can be up to O(m * n) rounds, so the total is quadratic in the number of cells; only a few extra variables are used.

What Trips People Up in Real Interviews

1

Crushing cells during the scan. A candy can be part of both a horizontal and a vertical run; clearing early hides the second run.

2

Comparing raw values after marking. Marked cells are negative — compare absolute values so marked cells still extend runs.

3

Treating 0 (empty) as a candy type. Three empty cells in a row are not a match.

4

Doing only one round. Falling candies can create new runs, so loop until nothing changes.

Solution Code

O((m * n)^2) time
def candyCrush(board):
    m, n = len(board), len(board[0])
    while True:
        found = False
        for r in range(m):
            for c in range(n - 2):
                v = abs(board[r][c])
                if v and v == abs(board[r][c + 1]) == abs(board[r][c + 2]):
                    board[r][c] = board[r][c + 1] = board[r][c + 2] = -v
                    found = True
        for r in range(m - 2):
            for c in range(n):
                v = abs(board[r][c])
                if v and v == abs(board[r + 1][c]) == abs(board[r + 2][c]):
                    board[r][c] = board[r + 1][c] = board[r + 2][c] = -v
                    found = True
        if not found:
            return board
        for c in range(n):
            w = m - 1
            for r in range(m - 1, -1, -1):
                if board[r][c] > 0:
                    board[w][c] = board[r][c]
                    w -= 1
            for r in range(w, -1, -1):
                board[r][c] = 0

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Candy Crush problem?

Candy Crush asks you to simulate the elimination rules of the game on a grid: any horizontal or vertical run of three or more identical candies is crushed at the same time, candies above empty cells fall down, and the process repeats until the board is stable. It is a pure simulation question that tests careful, bug-free grid code.

How do you solve Candy Crush?

The optimal approach runs in O((m * n)^2) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Candy Crush?

Candy Crush is asked at Roblox. It is a medium difficulty problem.

What are common mistakes on Candy Crush?
  • Crushing cells during the scan. A candy can be part of both a horizontal and a vertical run; clearing early hides the second run.
  • Comparing raw values after marking. Marked cells are negative — compare absolute values so marked cells still extend runs.
  • Treating 0 (empty) as a candy type. Three empty cells in a row are not a match.
  • Doing only one round. Falling candies can create new runs, so loop until nothing changes.