FastPrepCount 2x2 Submatrices by Black Cells
Problems/Count 2x2 Submatrices by Black Cells

Count 2x2 Submatrices by Black Cells

TikTok logoTikTok● MediumINTERNOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: A TikTok Intern OA report last seen on 2026-08-05 describes sparse counting for 2 x 2 blocks: each marked cell affects at most four blocks, and untouched blocks are recovered from the total. The existing HRT problem Count 2x2 Submatrices by Black Cells has the same reported core and was used to complete missing callable and output details; it is a cross-company reference, not evidence that TikTok used identical wording. Because the report omits the original callable signature, examples, and full output contract, this judged version returns the standard length-five distribution for blocks containing 0 through 4 black cells. The reported core behavior is preserved at about 90% confidence.

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 = 3cols = 3black = [[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.

More TikTok problems

  • Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
  • Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Oct 2026
  • Next Greater Indices and Maximum GapPHONE SCREEN · Seen Oct 2026
All TikTok interview questionsSee TikTok hiring insights
Saved
class Solution {
    public long[] solution(int rows, int cols, int[][] black) {
        // write your code here
    }
}
Checking account…