Coordinate Removals with Column Gravity
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure is the whole game in this ZipRecruiter OA, reported in September 2024. You get a grid, a list of up to 100000 coordinates, and a gravity rule that shifts a column down after each removal. Brute force looks obvious and it will bite you. This is a simulation problem, but the winning move is picking a per-column structure that makes each removal cheap. If you blank on that choice, StealthCoder runs invisibly during the live assessment and can hand you the approach, but the idea is simple enough to own before you start.
The problem
matrix contains distinct positive values; zero represents empty. Process zero-based [row,column] coordinates in order against the evolving matrix. If the addressed cell is nonzero, remove it, shift every value above it down by one in that column, and set the top cell to zero. If it is already zero, skip it. Return the final matrix. Function removeCellsWithGravity(matrix: int[][], coordinates: int[][]) → int[][] Examples Example 1 matrix = [[1,2],[3,4]] coordinates = [[1,0]] return = [[0,2],[1,4]] The value above falls into the removed bottom cell. Example 2 matrix = [[1],[2],[3]] coordinates = [[0,0]] return = [[0],[2],[3]] Removing the top simply replaces it with zero. Constraints 1 <= rows, columns <= 500 0 <= coordinates.length <= 100000 Every coordinate is in bounds.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Naive simulation shifts up to 500 cells per removal. With 100000 coordinates that's about 50 million operations at worst, which actually fits. So the plain approach can pass, and the real trap is bugs, not speed. Shift from the removed row upward: for r from row down to 1, set matrix[r][col] = matrix[r-1][col], then set matrix[0][col] = 0. Get the direction wrong and you overwrite values you still need. Another pitfall is skipping the zero check, which would wrongly shift the column when the cell is already empty. A faster option keeps each column as a list of nonzero values stacked from the bottom, but then coordinate mapping gets tricky because rows are physical positions. Check the two examples by hand before submitting. If the live OA catches you second-guessing the loop direction, StealthCoder is the hedge.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Coordinate Removals with Column 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Coordinate Removals with Column Gravity FAQ
How hard is this ZipRecruiter problem really?+
Easy to medium. There's no clever algorithm. It's careful simulation on a grid. The difficulty is getting the shift direction right and handling already-empty cells. Most candidates who trace the examples first finish quickly.
What's the trick to the column gravity shift?+
Work only inside the affected column. Starting at the removed row, copy the value from the row above into the current row, moving upward, then zero the top cell. Only do this when the target cell is nonzero.
Will brute force time out with 100000 coordinates?+
Probably not. Each removal touches at most 500 cells, so worst case is roughly 50 million simple operations. That's usually fine. Still, avoid extra allocations or copying the whole matrix per step.
What edge cases should I test?+
Removing the top row cell, removing the same coordinate twice, a single-row matrix, a single-column matrix, and an empty coordinates list. The second removal of the same spot may hit a different value or a zero after shifting.
How do I prepare for this in 48 hours?+
Practice grid simulation with in-place updates. Write the column shift loop from memory, then trace both examples by hand. Focus on loop bounds and update order, since that's where the bugs live.