Count 2x2 Submatrices by Black Cells
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The TikTok OA reported in August 2026 hands you a grid up to 100000 by 100000 and at most 500 black cells. The whole problem hinges on a hash map, because you can't touch the grid itself. You count black cells per 2x2 block by storing only the blocks that matter. If you see this in your invite window, the pattern is hash-table counting with a subtraction at the end. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea is short enough to hold in your head tonight.
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: each black cell touches at most 4 submatrices, identified by upper-left corner (r-1..r, c-1..c). For each black cell, loop over those 4 corners, skip any outside [0, rows-2] x [0, cols-2], and increment a map keyed by corner. After processing all 500 cells, the map holds every block with at least one black cell. Tally the map values into result[1..4]. Then result[0] equals (rows-1)*(cols-1) minus the number of blocks in the map. The common pitfall is overflow. (rows-1)*(cols-1) reaches about 10^10, so use a 64-bit type, which is why the return is long[]. Another pitfall is forgetting the bounds check on corners, which double counts or crashes on edges. Complexity is O(n) time with n at most 500. If you freeze during the live OA, StealthCoder can surface this map approach for you, but the logic is only about fifteen lines.
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 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. 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
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 TikTok's OA.
TikTok 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.
Count 2x2 Submatrices by Black Cells FAQ
How hard is this TikTok OA problem really?+
Easy to medium. The algorithm is simple once you stop thinking about the full grid. The difficulty is noticing the grid is too big to build and that only 500 cells matter. Most failures come from overflow or edge bounds, not from the idea.
What's the trick to solve it fast?+
Map each black cell to the up to 4 upper-left corners of 2x2 blocks that contain it. Count hits per corner in a hash map. Blocks with k hits have k black cells. Blocks not in the map have zero, so compute that by subtraction.
Why does result[0] need special handling?+
Almost every block is empty, and there can be about 10^10 of them, so you can't enumerate them. Compute total blocks as (rows-1)*(cols-1) in 64-bit, then subtract the number of distinct blocks that appear in your map.
What edge cases should I test before submitting?+
Test empty black array, which gives total blocks in slot 0 and zeros elsewhere. Test cells on the first and last row and column so corner bounds are checked. Test a full 2x2 of black cells for a result of 4. Test max-size rows and cols for overflow.
How do I prepare for this in 48 hours?+
Write the solution once from scratch with a hash map keyed by r*cols+c or a pair. Run example 1 by hand. Then review similar sparse-grid counting problems where you only store touched cells. Don't memorize code, memorize the corner-offset idea.