FastPrepMaximal Square
Problems/Maximal Square

Maximal Square

Salesforce logoSalesforce● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Given an m x n binary matrix containing the characters '0' and '1', return the area of the largest square whose cells are all '1'.

Function

maximalSquare(matrix: char[][]) → int

Examples

Example 1

matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]return = 4

The largest all-one square has side length 2, so its area is 4.

Example 2

matrix = [["0","1"],["1","0"]]return = 1

Each '1' cell forms a square of side length 1, and no larger all-one square exists.

Example 3

matrix = [["0"]]return = 0

The matrix contains no '1' cell, so the maximum area is 0.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 300
  • Each matrix[i][j] is either '0' or '1'.

More Salesforce problems

  • Count Subarrays With Product Equal to KOA · Seen Nov 2026
  • Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
  • Get Message StatusOA · Seen Nov 2026
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public int maximalSquare(char[][] matrix) {
      // write your code here
    }

}
Checking account…