Count All-One Squares
Updated
Problem statement
Given a nonempty rectangular binary matrix, return the number of square submatrices whose cells are all 1.
A square uses consecutive rows and consecutive columns. Count each distinct top-left position and side length once, including 1 x 1 squares. Overlapping squares are counted separately. Return a count of squares, rather than the largest area or the sum of their areas.
Function
countSquares(matrix: int[][]) → intExamples
Example 1
matrix = [[1,1],[1,1]]return = 5Four 1x1 squares and the one 2x2 square contribute five squares.
Example 2
matrix = [[1,1,1],[1,1,1]]return = 8Six 1x1 squares and two overlapping 2x2 squares are counted.
Example 3
matrix = [[1,0],[0,1]]return = 2Only the two isolated 1 cells are valid squares.
Constraints
1 <= matrix.length, matrix[0].length <= 100.- Every row has the same length.
- Every entry is
0or1.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Maximum All-Ones Square Side LengthOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Enumerate Right-and-Down Matrix PathsOracle · ONSITE INTERVIEWOracle interview questions
- Maximal Square AreaUber · ONSITE INTERVIEWUber interview questions
- Minimum Path SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximum Grid Path ScoreDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Minimum Path SumBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Bomb Enemy Best CoordinatesSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
- Maximum Falling Path Sum in a RectanglePeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions