FastPrepMaximal Rectangle in a Binary Matrix
Problems/Maximal Rectangle in a Binary Matrix

Maximal Rectangle in a Binary Matrix

Google logoGoogle● HardNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the task of the maximum rectangle in a binary matrix and the row-histogram approach with a monotonic stack. We added the function signature and the input limits. Our version is about 96% the same as the reported question.

Given a rectangular binary matrix matrix, return the area of the largest axis-aligned rectangle composed only of 1 values.

A rectangle may span any positive number of consecutive rows and columns. Return 0 when the matrix contains no 1.

Function

maximalRectangle(matrix: int[][]) → 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 = 6
matrix
01234010100110111211111310010

The largest all-one rectangle has height 2 and width 3.

Example 2

matrix = [[0]]return = 0
matrix
000

No all-one rectangle exists.

Example 3

matrix = [[1,1],[1,1]]return = 4
matrix
01011111

The entire matrix is an all-one rectangle of area 4.

Constraints

  • 1 <= matrix.length <= 200.
  • 1 <= matrix[i].length <= 200.
  • Every row has the same length.
  • Every value is either 0 or 1.

More Google Onsite Interview questions

  • 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
  • Route Pattern MatcherPHONE SCREEN · ONSITE 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

  • Dungeon GameAmazon · ONSITE INTERVIEW
  • Longest Increasing Path in a MatrixNew Relic · PHONE SCREEN · ONSITE INTERVIEW
  • Longest Increasing Path in a MatrixVisa · ONSITE INTERVIEWVisa interview questions
  • Trapping Rain WaterMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Maximum All-Ones Square Side LengthOmnissa · ONSITE INTERVIEWOmnissa interview questions
  • Minimum Path SumBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Bomb Enemy Best CoordinatesSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
  • Largest Square of Ones in a GridExpedia · ONSITE INTERVIEWExpedia interview questions
  • Cherry PickupInfosys · OAInfosys interview questions
  • Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int maximalRectangle(int[][] matrix) {
        // Write your code here.
    }
}
Checking account…