Nonzero Local Maxima in Cornerless Neighborhoods
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reported this one in October 2023, and the input size is the first thing to read. A matrix up to 1000 by 1000 means a million cells, so anything beyond a constant check per cell is a problem. The task is a matrix scan: find every nonzero cell strictly greater than its in-bounds up, down, left and right neighbors. Corners of the 3 by 3 window are ignored. If you're taking this OA soon, the logic is easy and the traps are in the edge cases. StealthCoder is the safety net running invisibly during the live OA if you blank on the details.
The problem
For each nonzero cell, consider its in-bounds orthogonal neighbors, the centered 3 by 3 neighborhood with all four corners excluded. The cell is a local maximum when its value is strictly greater than every such neighbor. Return zero-based [row,column] coordinates of all local maxima in row-major order. A nonzero cell with no orthogonal neighbor qualifies. Function cornerlessLocalMaxima(matrix: int[][]) → int[][] Examples Example 1 matrix = [[1,2,1],[2,9,2],[1,2,1]] return = [[1,1]] The center exceeds all four orthogonal neighbors. Example 2 matrix = [[5,5]] return = [] Strict comparison rejects both equal cells. Constraints 1 <= rows, columns <= 1000 -1000000000 <= matrix[r][c] <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there isn't one. It's a direct simulation. Walk every cell in row-major order, skip zeros, and compare against at most four neighbors using a direction array. Total work is about four million comparisons at most, which is nothing. Brute force here means rescanning a full 3 by 3 window with extra logic, but even that is constant per cell. The real danger is correctness. Use strict greater-than, so equal neighbors disqualify the cell, as Example 2 shows. Skip out-of-bounds neighbors instead of treating them as zero or negative. A nonzero cell with no neighbors, like a 1 by 1 matrix, qualifies automatically. Negative values count as nonzero, so don't filter on value greater than zero. Row-major scanning gives you the required output order for free. If you freeze on bounds handling during the live OA, StealthCoder can hand you the loop structure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Nonzero Local Maxima in Cornerless Neighborhoods 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Nonzero Local Maxima in Cornerless Neighborhoods FAQ
How hard is the ZipRecruiter cornerless local maxima problem really?+
Easy. It's a single pass over the grid with four neighbor checks per cell. Nothing needs optimizing past O(rows times columns). Most failures come from edge handling, not from the algorithm itself.
What's the trick to this problem?+
There's no clever trick. Use a direction array for up, down, left, right, skip out-of-bounds neighbors, and compare strictly. Scan in row-major order so results come out sorted without any extra sorting step.
Do negative numbers count as nonzero?+
Yes. Only cells equal to zero are skipped. A cell with value -5 is nonzero and can be a local maximum if every neighbor is strictly smaller, for example -7. Don't write a check like value greater than zero.
What happens with a single cell or a single row?+
A 1 by 1 nonzero cell has no neighbors, so it qualifies. In a single row, only left and right neighbors exist. Example 2 with [[5,5]] returns empty because equal values fail the strict comparison.
How do I prepare for this in 48 hours?+
Write the grid-neighbor loop from memory a couple of times with bounds checks. Test it on a 1 by 1 grid, a single row, equal neighbors, and negatives. That covers nearly every way this problem breaks.