Count 2x2 Submatrices by Black Cells
Reported by candidates from Hudson River Trading's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Hudson River Trading reported this one in July 2026, and the whole question comes down to one data structure: a hash map. You get a grid up to 10^5 by 10^5 and at most 500 black cells, then you count 2x2 submatrices by how many black cells they hold. Don't touch the grid. It's far too big to build. If you're taking this OA soon, learn the sparse trick below and you're mostly done. StealthCoder runs invisibly as a safety net on the live OA if your mind goes blank on the counting step.
The problem
You are given a black-and-white grid with rows rows and cols columns. The array black contains the [row, column] coordinates of every black cell in the grid. All other cells are white, and the black-cell coordinates are pairwise unique. For every blackCount from 0 through 4, count how many 2 x 2 submatrices contain exactly blackCount black cells. Return an array result of length 5, where result[i] is the number of 2 x 2 submatrices containing exactly i black cells. Function solution(rows: int, cols: int, black: int[][]) → long[] Examples Example 1 rows = 3 cols = 3 black = [[0, 0], [0, 1], [1, 0]] return = [1, 2, 0, 1, 0] There are four 2 x 2 submatrices: The submatrix with upper-left corner (0, 0) contains 3 black cells. The submatrix with upper-left corner (0, 1) contains 1 black cell. The submatrix with upper-left corner (1, 0) contains 1 black cell. The submatrix with upper-left corner (1, 1) contains 0 black cells. Therefore, the counts for 0 through 4 black cells are [1, 2, 0, 1, 0]. Constraints 2 <= rows <= 10^5 2 <= cols <= 10^5 0 <= black.length <= 500 black[i].length = 2 0 <= black[i][0] < rows 0 <= black[i][1] < cols All coordinates in black are pairwise unique.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to iterate over black cells, not submatrices. Each black cell (r,c) touches at most four 2x2 submatrices, identified by upper-left corners (r-1,c-1), (r-1,c), (r,c-1), (r,c). Skip any corner outside 0..rows-2 and 0..cols-2. Use a hash map from corner to black count, and increment it for each valid corner. After that, every corner in the map has a count from 1 to 4. Tally those into result[1..4]. For result[0], compute total submatrices (rows-1)*(cols-1) and subtract the number of corners in the map. The common pitfall is overflow. Total can reach about 10^10, so use a 64-bit integer, which is why the return type is long. Another trap is forgetting the boundary check and counting corners that don't exist. Encode the key as r*cols+c in a long to keep hashing fast. Work is O(500) with at most 2000 map entries. If you freeze on the zero-count formula during the live OA, 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 Count 2x2 Submatrices by Black Cells 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
This OA pattern shows up on LeetCode as number of black blocks. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Hudson River Trading's OA.
Hudson River Trading 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.
Count 2x2 Submatrices by Black Cells FAQ
What's the trick in the Hudson River Trading 2x2 black cells problem?+
Never build the grid. Each black cell affects only four 2x2 submatrices, so loop over the black cells, bump a hash map keyed by upper-left corner, then tally the counts. Zero-black submatrices come from the total minus the map size.
How do I compute the count of submatrices with zero black cells?+
The total number of 2x2 submatrices is (rows-1)*(cols-1). Every corner stored in your map has at least one black cell. So result[0] equals the total minus the number of distinct keys in the map. Use long math to avoid overflow.
Why does the return type use long?+
With rows and cols up to 10^5, the total submatrix count is near 10^10, which exceeds a 32-bit int. The zero-black bucket is usually the huge one. Use 64-bit integers for the total and for result[0], or you'll get wrong answers on large tests.
What edge cases break this solution?+
Black cells on the border are the main one. A cell at row 0 can't be the bottom row of a submatrix with corner r-1, so you must bounds-check each of the four corners. Also handle an empty black array, which should return all submatrices in result[0].
How should I prepare for this in 48 hours?+
Practice the sparse-counting idea: map each item to the few buckets it affects, then aggregate. Write this solution once from scratch with a long-encoded key, test the 3x3 example from the prompt, and check border cases. That covers it.