Maximal Square

MediumDynamic Programming2d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an m x n grid matrix whose cells are the characters "0" or "1". Find the largest axis-aligned square made entirely of "1" cells and return its **area**. If the grid has no "1" at all, return 0.

Examples

Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 4
The 2x2 block in rows 1-2, columns 2-3 (or 3-4) is all ones.
Input: matrix = [["0","1"],["1","0"]]
Output: 1
Only single cells of ones exist.
Input: matrix = [["0"]]
Output: 0
There are no ones.

Constraints

  • 1 <= m, n <= 300
  • `matrix[i][j]` is `"0"` or `"1"`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 2d dp problems