Reported September 2022
ZipRecruitermatrix

Remove Equal Components and Apply Gravity

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

A 200 by 200 grid means 40,000 cells, and that size is exactly why a sloppy re-scan after every removal won't survive. ZipRecruiter reported this one in September 2022: remove every connected group of equal positive values with at least three cells, then drop everything down by gravity. It's flood fill plus column compaction, and the word "simultaneous" is where people lose points. If you blank on the structure during the live OA, StealthCoder runs invisibly as a safety net and gives you the solution while you keep typing.

The problem

Positive cells of equal value form components through shared edges; zero is empty. In one simultaneous round, remove every component containing at least three cells by setting it to zero.
Then compact each column downward toward larger row indices, preserving the top-to-bottom order of surviving values. Return the resulting matrix.

Function
removeComponents(grid: int[][]) → int[][]

Examples
Example 1
grid = [[1,1,1],[2,0,3],[4,5,6]]
return = [[0,0,0],[2,0,3],[4,5,6]]
The three ones disappear and columns compact downward.
Example 2
grid = [[2,1],[2,3],[2,4]]
return = [[0,1],[0,3],[0,4]]
A vertical three-cell component is removed.

Constraints
1 <= rows, columns <= 200
0 <= grid[r][c] <= 1000000000

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two clean passes. Pass one: run DFS or BFS over the original grid with a visited array, collect each component of equal positive values, and mark it for removal if its size is 3 or more. Don't zero anything yet. Zeroing as you go breaks the simultaneous rule and can merge or split components wrongly. Pass two: for each column, walk from the bottom row to the top, copy surviving nonzero values down with a write pointer, then fill the rest with zeros. That keeps top-to-bottom order. Total work is O(rows * cols). The pitfalls are recursion depth (a 40,000-cell component can overflow the stack, so use an iterative stack or queue), treating zero as a component, and checking only 4-direction neighbors. StealthCoder is your hedge in the live OA if the iterative flood fill or the pointer compaction slips on you.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Remove Equal Components and Apply 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Remove Equal Components and Apply Gravity FAQ

What's the trick in the ZipRecruiter remove components and gravity problem?+

Separate detection from mutation. Find all components on the untouched grid, record cells from groups of size 3 or more, then zero them all at once. After that, compact each column with a write pointer from the bottom. Mixing the steps is the most common bug.

Do I need DFS or BFS here?+

Either works. With a 200 by 200 grid, recursive DFS can hit stack limits on a large single component, so BFS with a queue or DFS with an explicit stack is safer. Both run in O(rows * cols) with a visited array.

Does the removal happen once or repeat until stable?+

Per the problem text, it's one simultaneous round. You remove all components of size 3 or more, then apply gravity once, and return the matrix. Don't loop. Falling cells forming new groups doesn't trigger another removal.

How do I do the gravity step correctly?+

Process each column independently. Start a write index at the last row, scan rows from bottom to top, and when you see a nonzero value, place it at the write index and move the index up. Fill the remaining top cells with zero. Order is preserved.

How should I prepare for this in 48 hours?+

Practice flood fill on a grid until it's automatic: visited array, four directions, bounds checks, component size counting. Then write the column compaction separately. Test both given examples, including the vertical three-cell case, and an all-zero grid.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it