Candy Crush Grid Matching and Gravity
Reported by candidates from Roblox's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The grid tops out at 50 x 50, so 2,500 cells. That's the detail that tells you brute force is fine here and the real risk is bookkeeping. Roblox reported this Candy Crush crush-and-gravity question in September 2026, and it's a grid simulation more than a true search problem. You mark runs of three or more, zero them, then drop columns. Candidates lose points on overlapping matches, not on speed. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and reads the problem for you. Know the marker-grid idea cold and this is a quick one.
The problem
You are given an m x n grid board of non-negative integers. A positive value represents a candy type, and 0 represents an empty cell. A match is any horizontal or vertical run of at least three adjacent cells with the same positive value. Part 1, find matches. Identify every horizontal and vertical run of length at least 3. Scan start cells in row-major order (top-left to bottom-right). A vertical run is only started from a cell when the cell above it differs or is out of bounds; a horizontal run is only started when the cell to the left differs or is out of bounds. Several reports note a direction-priority requirement: report all vertical matches before horizontal ones. Part 2 (the main ask), crush and apply gravity. Return the board after one crush pass: remove every cell that belongs to at least one match (set it to 0), then apply gravity independently in each column so that non-zero values fall downward while preserving their relative order within the column, and empty cells at the top become 0. Important: mark all matched cells first (using a separate marker grid) before zeroing anything, so overlapping and adjacent matches are all detected against the original board. Follow-up: repeat the crush-and-gravity step until the board is stable (no run of length >= 3 remains), which is the full LeetCode 723 "Candy Crush" variant. Function crushOnce(board: int[][]) → int[][] Examples Example 1 board = [[1, 2, 2, 2], [1, 3, 4, 5], [1, 3, 3, 3], [6, 7, 8, 9]] return = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 3, 4, 5], [6, 7, 8, 9]] Matches: the first column has three 1s vertically, row 0 has three 2s horizontally, and row 2 has three 3s horizontally. Those matched cells are removed, then each column compacts downward: column 0 keeps only 6, column 1 keeps 3 then 7, column 2 keeps 4 then 8, column 3 keeps 5 then 9, with zeros filling the top. Example 2 board = [[1, 1, 1], [2, 4, 5], [2, 6, 7]] return = [[0, 0, 0], [2, 4, 5], [2, 6, 7]] Row 0 is a horizontal run of three 1s and is crushed to zeros. The two 2s in column 0 form a run of length 2 only, so they are not a match and survive. After crushing the top row, gravity pulls each column's surviving values down: column 0 already has its 2s at the bottom, and columns 1 and 2 are unaffected. Example 3 board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] return = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] There is no run of three equal positive values in any row or column, so nothing is crushed and the board is returned unchanged. Constraints 1 <= m, n <= 50 0 <= board[i][j] (a positive value is a candy type; 0 is empty). A match is a horizontal or vertical run of at least 3 equal positive values. Gravity is applied per column, preserving the relative order of surviving values.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two phases. First, scan every cell and mark matches in a separate boolean grid, checking horizontal and vertical runs against the original board. Never zero cells while scanning, or an overlapping L or T shaped match breaks and you miss cells. Second, apply gravity per column with a write pointer starting at the bottom row. Walk upward, copy non-zero values down, then fill the rest with 0. The common pitfall is mutating the board early. Another is forgetting that a run of exactly two survives. The follow-up loops this until stable, which is the full LeetCode 723 version, so write crushOnce as a clean function you can call repeatedly. Cost is O(m*n) per pass. If the live OA goes sideways, StealthCoder is the hedge that gives you a working implementation fast.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Candy Crush Grid Matching and Gravity cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as candy crush. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Roblox's OA.
Roblox reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Candy Crush Grid Matching and Gravity FAQ
How hard is the Roblox Candy Crush OA really?+
Easy to medium. There's no clever algorithm, just careful simulation. The grid is at most 50 x 50, so O(m*n) per pass is plenty. Most failures come from zeroing cells too early or botching the gravity pointer, not from complexity.
What's the trick to detecting overlapping matches?+
Use a separate marker grid. Scan the untouched board for horizontal and vertical runs of 3 or more, mark every cell in each run, and only zero out marked cells after the full scan finishes. That catches crosses, L shapes, and adjacent runs correctly.
How do I implement gravity cleanly?+
Handle each column on its own. Keep a write index at the bottom row, walk upward, and copy each non-zero value to the write index, then decrement it. After the walk, fill every remaining cell above the write index with 0. Order is preserved automatically.
Do I need to handle the repeat-until-stable follow-up?+
Be ready. The follow-up repeats crush and gravity until no run of 3 remains, which is LeetCode 723. If crushOnce returns whether anything changed, or you compare boards, the loop is a few lines. Build the first part as a reusable function.
How do I prepare in 48 hours?+
Write the marker-grid crush and the column gravity from scratch twice, timed. Test the three examples, plus a case with a cross-shaped overlap and a run of exactly two. Also note the vertical-before-horizontal reporting order if the part 1 output is asked for.