Reported October 2023
ZipRecruiterprefix sum

Partition Matrix by Nonnegative Averages

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 200x200 matrix means 40,000 cells and roughly 40,000 possible splits, so recomputing four regions from scratch per split is the trap. ZipRecruiter reported this one in October 2023, and it looks like a search problem but it's really 2D prefix sums in disguise. You pick a row and column, get four regions, floor the average of the nonnegative cells in each, and minimize the spread. If the prefix-sum idea doesn't click when the timer is running, StealthCoder is the safety net that runs invisibly during the live OA and hands you the structure.

The problem

For this exercise, use the callable contract below.
You are given an integer matrix with at least two rows and two columns. Choose a row index r and a column index c that divide the matrix into four non-empty rectangular regions:
top-left: rows 0 through r and columns 0 through c;
top-right: rows 0 through r and columns c + 1 through the last column;
bottom-left: rows r + 1 through the last row and columns 0 through c;
bottom-right: rows r + 1 through the last row and columns c + 1 through the last column.
For each region, ignore negative entries and compute the floor of the average of its nonnegative entries. A split is valid only when every region contains at least one nonnegative entry.
Minimize the difference between the largest and smallest of the four floored averages. If several splits have the same minimum difference, return the one with the smallest r, then the smallest c.
Return [r, c].

Function
findBestPartition(matrix: int[][]) → int[]

Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]
return = [0,0]
The splits at [0,0], [1,0], and [1,1] each have range 6, the minimum possible range. The row-then-column tie rule selects [0,0].
Example 2
matrix = [[0,10],[4,8]]
return = [0,0]
The only split creates four one-cell regions, all with nonnegative entries, so the result is [0,0].

Constraints
2 <= matrix.length <= 200
2 <= matrix[i].length <= 200
Every row has the same length.
-10^9 <= matrix[i][j] <= 10^9
At least one valid split exists.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build two 2D prefix arrays: one for the sum of nonnegative entries (treat negatives as 0) and one for the count of nonnegative entries. Then any region's sum and count come out in O(1) with inclusion-exclusion. Loop over every r in 0..rows-2 and c in 0..cols-2, compute the four floored averages, skip the split if any count is 0, and track max minus min. That's O(rows*cols) total. Pitfalls: sums reach 40,000 * 10^9, so you need 64-bit integers. Negatives are ignored, not clamped into the average, so the count must exclude them. Floor division is on nonnegative numbers, so plain integer division is safe. Iterating r then c in increasing order and only updating on strictly smaller range handles the tie-break for free. The BFS hint doesn't apply here. StealthCoder is your hedge if the inclusion-exclusion indexing goes sideways live.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Partition Matrix by Nonnegative Averages 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 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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Partition Matrix by Nonnegative Averages FAQ

What's the trick in this ZipRecruiter partition problem?+

Use two 2D prefix arrays, one for the sum of nonnegative values and one for the count of nonnegative values. Every region's average becomes an O(1) lookup, so you can test all splits in O(rows*cols) instead of rescanning the matrix each time.

Why does brute force fail here?+

There are up to about 40,000 splits, and each one covers up to 40,000 cells. That's around 1.6 billion operations at the limits. Prefix sums cut the per-split work to a handful of arithmetic operations, which is what the constraints are pushing you toward.

What edge cases break most solutions?+

Regions with zero nonnegative entries must make the split invalid. Also watch overflow, since sums can exceed 32-bit range. And negatives contribute to neither sum nor count. Forgetting any of these gives wrong answers on hidden tests even when the examples pass.

How does the tie-break work?+

Iterate r from smallest to largest, and c from smallest to largest inside it. Only replace your best answer when the new range is strictly smaller. The first split that achieves the minimum range is then automatically the smallest r, then smallest c.

How do I prepare for this in 48 hours?+

Write 2D prefix sum from memory until the inclusion-exclusion formula is automatic. Then practice a variant with a second prefix array for counts. Test with a matrix of all negatives plus a few positives to confirm your validity check works.

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