Count 2x2 sub-matrices by number of black cells in a grid

Quick Overview

Given the size of a grid and the coordinates of its black cells, count how many 2x2 sub-matrices contain exactly zero, one, two, three or four black cells. Tests grid iteration, boundary handling and how to count efficiently when most cells are white.

Count 2x2 sub-matrices by number of black cells in a grid

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a grid with `m` rows and `n` columns and a list `coordinates` of the black cells. Every cell not listed in `coordinates` is white. Count how many `2x2` sub-matrices contain exactly 0, 1, 2, 3 or 4 black cells. Return an array of length 5 where element `i` is the number of `2x2` sub-matrices with exactly `i` black cells. ### Function Signature ```python def count_black_submatrices(m: int, n: int, coordinates: list[list[int]]) -> list[int]: ``` ### Rules - Each coordinate is a `[row, column]` pair, zero-based. Row 0 is the top row and column 0 is the left column. - A `2x2` sub-matrix is two adjacent rows and two adjacent columns that lie fully inside the grid. It is identified by its top-left cell `(r, c)` with `0 <= r <= m - 2` and `0 <= c <= n - 2`, so there are `(m - 1) * (n - 1)` of them. Overlapping sub-matrices are counted separately, and the five counts add up to `(m - 1) * (n - 1)`. ### Constraints - `2 <= m, n <= 1000` - `0 <= len(coordinates) <= m * n` - Each coordinate `[r, c]` satisfies `0 <= r < m` and `0 <= c < n`. - All coordinates are unique. ### Examples ```text Input: m = 3, n = 3, coordinates = [[0, 0], [1, 1], [2, 2]] Output: [0, 2, 2, 0, 0] B W W W B W W W B ``` The black cells lie on the main diagonal (B is black, W is white). Of the four `2x2` sub-matrices, the top-left and bottom-right ones each hold 2 black cells, and the top-right and bottom-left ones each hold 1. ```text Input: m = 2, n = 2, coordinates = [[0, 0], [0, 1], [1, 0], [1, 1]] Output: [0, 0, 0, 0, 1] B B B B ``` The grid is a single `2x2` sub-matrix, and all four of its cells are black. ```text Input: m = 2, n = 3, coordinates = [] Output: [2, 0, 0, 0, 0] ``` A 2 by 3 grid has two `2x2` sub-matrices, and with no black cells both hold 0.

Overview: Given the size of a grid and the coordinates of its black cells, count how many 2x2 sub-matrices contain exactly zero, one, two, three or four black cells. Tests grid iteration, boundary handling and how to count efficiently when most cells are white.

Stripe logo
Stripe
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a grid with m rows and n columns and a list coordinates of the black cells. Every cell not listed in coordinates is white. Count how many 2x2 sub-matrices contain exactly 0, 1, 2, 3 or 4 black cells.

Return an array of length 5 where element i is the number of 2x2 sub-matrices with exactly i black cells.

Function Signature

def count_black_submatrices(m: int, n: int, coordinates: list[list[int]]) -> list[int]:

Rules

  • Each coordinate is a [row, column] pair, zero-based. Row 0 is the top row and column 0 is the left column.
  • A 2x2 sub-matrix is two adjacent rows and two adjacent columns that lie fully inside the grid. It is identified by its top-left cell (r, c) with 0 <= r <= m - 2 and 0 <= c <= n - 2 , so there are (m - 1) * (n - 1) of them. Overlapping sub-matrices are counted separately, and the five counts add up to (m - 1) * (n - 1) .

Constraints

  • 2 <= m, n <= 1000
  • 0 <= len(coordinates) <= m * n
  • Each coordinate [r, c] satisfies 0 <= r < m and 0 <= c < n .
  • All coordinates are unique.

Examples

Input: m = 3, n = 3, coordinates = [[0, 0], [1, 1], [2, 2]]
Output: [0, 2, 2, 0, 0]

B W W
W B W
W W B

The black cells lie on the main diagonal (B is black, W is white). Of the four 2x2 sub-matrices, the top-left and bottom-right ones each hold 2 black cells, and the top-right and bottom-left ones each hold 1.

Input: m = 2, n = 2, coordinates = [[0, 0], [0, 1], [1, 0], [1, 1]]
Output: [0, 0, 0, 0, 1]

B B
B B

The grid is a single 2x2 sub-matrix, and all four of its cells are black.

Input: m = 2, n = 3, coordinates = []
Output: [2, 0, 0, 0, 0]

A 2 by 3 grid has two 2x2 sub-matrices, and with no black cells both hold 0.