One round of candy crush on a grid
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's online assessment, last asked Sep 2025. It tests simulation and grids.
An m x n integer matrix holds coloured blocks: a positive value is a colour and 0 is empty. Simulate exactly one round of elimination. First find every block that explodes: a non-empty cell explodes if at least two of its in-bounds neighbours up, down, left and right have the same colour as it. Decide all explosions from the board as it was at the start of the round, then clear those cells at once. Finally apply gravity: in each column, the remaining blocks slide to the bottom in their original order, and the cells above them become 0. Return the board.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
crush_once(board: list[list[int]]) -> list[list[int]]— returns a new board; an empty board is returned unchanged.
Example
crush_once([[1, 1, 1],
[0, 1, 0],
[2, 2, 0]]) -> [[0, 0, 0],
[1, 1, 0],
[2, 2, 1]]
# only (0, 1) has two or more same-colour neighbours (it has three); after clearing it, columns fall
crush_once([[3, 1, 3],
[3, 3, 2],
[1, 3, 2]]) -> [[0, 0, 3],
[3, 1, 2],
[1, 3, 2]]
# (1, 0) and (1, 1) each have two neighbours of colour 3; the two cells above them fall
crush_once([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) -> unchanged
Constraints
-
1 <= m, n <= 1000; values are non-negative integers.
Clarifying questions
- What makes a block explode: two same-colour neighbours, or a run of three in a line? (Two neighbours here.)
- Do diagonal neighbours count? (No.)
- Is it one round only, or repeat until nothing explodes?
- Modify in place or return a new board?
What a strong answer covers
- Marking explosions in a separate pass (or a boolean grid) before clearing anything, so one removal cannot change another cell's count in the same round.
- Gravity per column with a write pointer from the bottom, keeping the order of surviving blocks.
- O(m · n) time; O(m · n) extra space for the marks, or O(1) by marking with negative values.
- Tests: no explosions, the whole board exploding, empty cells between blocks in a column.
Follow-ups
- Repeat rounds until the board is stable, and return the number of rounds.
- Use the classic rule instead: three or more in a horizontal or vertical line.
- Score each round by the number of cleared cells, with a bonus for chain reactions.
Where it's asked
- Uber · Online assessment · Last asked Sep 202590 min · Two problems on a HackerRank-based platform, camera and screen share on · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
