Largest All-One Square Area
Updated
Problem statement
You are given a nonempty rectangular binary matrix matrix. Find the largest square whose cells all contain 1, and return its area.
A square uses consecutive rows and consecutive columns. Return 0 when the matrix contains no 1.
Function
maximalSquareArea(matrix: int[][]) → intExamples
Example 1
matrix = [[1,1,1],[1,1,1]]return = 4A square can have side length 2, even though the all-one rectangle is wider.
Example 2
matrix = [[1,1,0],[1,1,1],[0,1,1]]return = 4There are two overlapping squares of side length 2; the whole matrix is not all ones.
Example 3
matrix = [[0,0],[0,0]]return = 0No all-one cell exists, so the largest square has area 0.
Constraints
1 <= matrix.length, matrix[0].length <= 300.- Every row has the same length.
- Every cell is
0or1.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Unique Grid PathsMorgan Stanley · PHONE SCREENMorgan Stanley interview questions
- Count Starts Reaching a Grid MaximumDuolingo · PHONE SCREENDuolingo interview questions
- Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions
- Factory Cost — Minimum Cost Across StagesStripe · PHONE SCREENStripe interview questions
- Path Through an O/X Grid Using Only Right and DownAmazon · PHONE SCREEN
- 3D Grid Diagonal Walk ProbabilityApple · PHONE SCREENApple interview questions
- Maximum Rocks on a North-or-East Grid PathGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Maximal SquareAirwallex · PHONE SCREENAirwallex interview questions
- Maximal SquareSalesforce · PHONE SCREENSalesforce interview questions
- Maximum Grid Path ScoreDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions