PPractick

DSA interview question

Maximal Rectangle (Largest Rectangle of All 1s in a Binary Matrix)

Hard · Algorithms · Topics: dynamic programming, stack, histogram

The problem

Back at the warehouse robotics team — the "square staging zones" analysis wasn't flexible enough for the operations team. They actually want the single largest rectangular zone (not necessarily square) that can be marked off entirely within occupied (1) tiles, to fit the biggest possible piece of equipment. Given a binary matrix, find the area of the largest rectangle containing only 1s.

Example: matrix = [ ["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","1","1"], ["1","0","0","1","0"] ] → largest rectangle has area 6 (formed by rows 1-2, columns 2-4).

Function signature

int maximalRectangle(vector<vector<char>>& matrix);

Test cases

Input matrixOutputExplanation
[["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]6Best rectangle: rows 1-2, cols 2-4 (2 rows x 3 cols)
[["0"]]0Single zero, no rectangle possible
[["1"]]1Single cell, area 1
[["1","1"],["1","1"]]4Whole 2x2 grid is all 1s
[["0","0"],["0","0"]]0No 1s anywhere
[["1","1","1"],["1","1","1"],["1","1","1"]]9Whole 3x3 grid is all 1s

How interviewers phrase it

Clarifying questions to ask

  1. Have I solved "Largest Rectangle in Histogram" before, or should I derive that subroutine from scratch? (Critical to clarify time budget — this problem is essentially two hard problems stacked together.)
  2. Is the matrix given as char ('0'/'1') or int (0/1)? (Matches LeetCode's exact signature using char — confirm input type to avoid type-mismatch bugs.)
  3. What are the matrix dimension constraints? (Determines whether O(m*n) overall is required — yes, almost certainly, given this is the expected optimal complexity.)
  4. Should I return just the area, or also the actual rectangle's coordinates/boundaries? (Standard version just wants area — confirm.)

Follow-ups to expect

  1. "Walk through how you convert this 2D problem into a series of 1D histogram problems."
  2. "Explain the monotonic stack approach for 'Largest Rectangle in Histogram' in detail."
  3. "Why does using a monotonic stack give you O(n) instead of O(n²) for each histogram, and how does that affect the overall complexity?"
  4. "Can you do this with O(n) space instead of O(m*n)?"
  5. "What's the relationship between this problem and 'Count Square Submatrices with All Ones' you solved earlier?"
  6. "Prove why the monotonic stack approach correctly finds the maximum rectangle, not just A valid rectangle."

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in