FastPrepNumber of Submatrices That Sum to Target
Problems/Number of Submatrices That Sum to Target

Number of Submatrices That Sum to Target

WhatNot logoWhatNot● HardINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An intern applicant interviewed with Whatnot between April and June 2024. Their third round was 45 minutes with a team member and had two questions, both around medium difficulty; this was the second, given as a changed version of the well-known task of counting submatrices with a given sum.

The poster does not say what was changed. Here you get the standard task: count every rectangle of cells whose values add up to the target, with negative values allowed.

Our version is about 60% the same as the reported question.

Given an m x n integer matrix matrix and an integer target, return how many non-empty submatrices have values that add up to exactly target.

A submatrix is a rectangle of cells, chosen by a top row r1, a bottom row r2, a left column c1 and a right column c2 with r1 <= r2 and c1 <= c2. It holds every cell matrix[r][c] with r1 <= r <= r2 and c1 <= c <= c2. Two submatrices are different when at least one of these four bounds differs, even if they hold the same values.

Values can be negative, zero or positive.

Function

numSubmatrixSumTarget(matrix: int[][], target: int) → int

Examples

Example 1

matrix = [[2,-1],[1,0]]target = 1return = 3
matrix
0102-1110

Three submatrices add up to 1: the single cell 1 at row 1, column 0; the whole top row [2, -1]; and the whole bottom row [1, 0].

Example 2

matrix = [[0,0],[0,0]]target = 0return = 9
matrix
01000100

Every one of the 9 submatrices adds up to 0: 4 single cells, 2 full rows, 2 full columns and the whole matrix. Equal values in different places still count as different submatrices.

Example 3

matrix = [[4,-2,3],[-1,5,-4]]target = 3return = 3
matrix
01204-231-15-4

Three submatrices add up to 3: the single cell 3 in the top row; the first column, 4 + (-1); and the second column, -2 + 5. The whole matrix adds up to 5, so it does not count.

Example 4

matrix = [[5]]target = 5return = 1
matrix
005

The only submatrix is the single cell, and it equals the target.

Example 5

matrix = [[1,2,3]]target = 7return = 0
matrix
0120123

The row adds up to at most 6, so no submatrix reaches 7.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -1000 <= matrix[i][j] <= 1000
  • -108 <= target <= 108

More WhatNot Onsite Interview questions

  • User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Detect Blocked Chat PhrasesPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
  • Minimum Remove to Make Valid ParenthesesONSITE INTERVIEW · Seen Dec 2024
  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2024
  • Merge Two Sorted ListsONSITE INTERVIEW · Seen Jul 2024
  • Parse and Store Log RecordsONSITE INTERVIEW · Seen Oct 2023
  • Room Occupancy at Query TimesONSITE INTERVIEW · Seen Oct 2023
  • K Smallest Values in a StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
  • Average Purchase PriceONSITE INTERVIEW · Seen Sep 2022

Similar questions at other companies

  • Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
  • Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions
  • Count Subarrays with Equal Even and Odd CountsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
  • Validate Rectangular Game PiecesGoogle · ONSITE INTERVIEWGoogle interview questions
  • Count Subarrays with Sum KMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
  • Centered K-by-K Submatrix SumsCitadel · ONSITE INTERVIEWCitadel interview questions
  • Query Whether an Island Size ExistsMeta · ONSITE INTERVIEWMeta interview questions
  • Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • Subarray Sum Equals KApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions

WhatNot interview experiences

  • Whatnot SWE New Grad Second Technical Interview: 8 Experiences
  • Whatnot interviews · October 2026
  • Whatnot Experienced SWE Phone Screen Questions: 2 Experiences
  • Whatnot SWE Interview Questions: 6 Experiences
All WhatNot interview questionsSee WhatNot hiring insights
Saved
class Solution {
    public int numSubmatrixSumTarget(int[][] matrix, int target) {
        // write your code here
    }
}
Checking account…