Partition Matrix by Nonnegative Averages
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The classic way to sink this TikTok OA, reported in September 2026, is to treat it as a grid search and recompute every region from scratch for each split. With a 200 by 200 matrix that's 40,000 splits, and brute force per split will time out. The hinted pattern is BFS, but the real engine here is 2D prefix sums over two arrays: nonnegative sums and nonnegative counts. If you blank on the setup, StealthCoder runs invisibly during the live OA as a safety net. Know the trick before you open the invite.
The problem
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 prefix-sum grids. One holds the sum of max(0, value) and the other holds the count of nonnegative cells. Any rectangle's sum and count then come out in O(1) by inclusion-exclusion. Loop r from 0 to rows-2 and c from 0 to cols-2. For each split, get the four regions, skip the split if any count is zero, compute floor(sum / count) for each, then take max minus min. Track the best range, and strict less-than comparison keeps the smallest r then c automatically because you iterate in order. Pitfalls: sums reach about 4e4 * 1e9 = 4e13, so use 64-bit integers. Negatives must be excluded from both sum and count, not just the sum. Floor division is safe since the sums are nonnegative. Total cost is O(rows * cols). If you freeze on the indexing, StealthCoder is the hedge during the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Partition Matrix by Nonnegative Averages FAQ
What's the actual trick in Partition Matrix by Nonnegative Averages?+
Two 2D prefix grids: one for the sum of nonnegative values, one for the count of nonnegative cells. Every region's average then costs O(1). Enumerate all splits, reject any with an empty region, and keep the smallest range of floored averages.
Is BFS really needed here?+
No. The hinted pattern says BFS, but nothing in the problem involves traversal or connectivity. It's an enumeration over splits with prefix sums. Don't burn time building a graph search for a problem that's just rectangle queries.
What's the most common mistake on this problem?+
Recomputing each region by looping over cells, which gives O(n^2 m^2) work and times out at 200 by 200. The second mistake is counting negative cells in the count or forgetting that a region with zero nonnegatives makes the split invalid.
How do I handle ties on the minimum difference?+
Iterate r ascending, then c ascending, and only update your best when the new range is strictly smaller. That preserves the smallest r then smallest c without extra comparison logic.
How do I prepare for this in 48 hours?+
Practice 2D prefix sums until the inclusion-exclusion formula is automatic. Then write this solution once end to end, using 64-bit sums and an extra padding row and column. Test it on the two given examples, including the single-split 2 by 2 case.