Quick Overview

Find the largest all-one square in a binary matrix using dynamic programming, with a correctness argument, boundary handling, and linear-space code.

Find the Largest All-One Square in a Binary Matrix

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given a rectangular matrix containing `0` and `1`, return the area of its largest square consisting entirely of ones. An empty matrix or a matrix with no ones has answer zero. ### Clarifying Questions to Ask - Is the desired result the square's area or its side length? - Are entries integers or character digits, and is the matrix guaranteed rectangular? ```hint Anchor one corner Describe the largest all-one square whose bottom-right corner is a particular cell. ``` ### What a Strong Answer Covers - A state measuring side length and a recurrence that checks all three adjacent directions. - Correct handling of zero cells and matrix boundaries. - Conversion from maximum side length to area and linear-space optimization. ### Follow-up Questions - How would you also return one square's coordinates? - Why is taking the maximum of neighboring square sizes incorrect?

Overview: Find the largest all-one square in a binary matrix using dynamic programming, with a correctness argument, boundary handling, and linear-space code.

Read the full Amazon Software Engineer interview experience this question came from

Given a rectangular matrix of integer zeros and ones, return the area of its largest contiguous square whose entries are all ones. Return 0 if the matrix is empty, has zero columns, or contains no ones.

Constraints

  • Every row has the same length and every entry is the integer 0 or 1.
  • The number of rows, the number of columns, and the total number of entries are each at most 2^31 − 1.

Examples

Input: ([[0, 1, 1, 0], [1, 1, 1, 1], [1, 1, 0, 1]],)

Expected Output: 4

Explanation: Rows 1 and 2 and columns 2 and 3 form a 2-by-2 square when numbered from 1. No 3-by-3 square is full of ones.

Input: ([[1, 1, 1, 1]],)

Expected Output: 1

Explanation: The single row limits every square to side length 1.

Hints

  1. A square occupies the same number of consecutive rows and columns.

Largest All-One Square Area

mediumAmazonSoftware Engineer

Given a rectangular matrix of integer zeros and ones, return the area of its largest contiguous square whose entries are all ones. Return 0 if the matrix is empty, has zero columns, or contains no ones.

Examples

Example 1
Input
([[0, 1, 1, 0], [1, 1, 1, 1], [1, 1, 0, 1]],)
Output
4
Notes
Rows 1 and 2 and columns 2 and 3 form a 2-by-2 square when numbered from 1. No 3-by-3 square is full of ones.
Example 2
Input
([[1, 1, 1, 1]],)
Output
1
Notes
The single row limits every square to side length 1.

Constraints

  • •Every row has the same length and every entry is the integer 0 or 1.
  • •The number of rows, the number of columns, and the total number of entries are each at most 2^31 − 1.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def largest_all_one_square_area(matrix: list[list[int]]) -> int:
Sample call
largest_all_one_square_area([[0, 1, 1, 0], [1, 1, 1, 1], [1, 1, 0, 1]])
Language
Case 1
Input
([[0, 1, 1, 0], [1, 1, 1, 1], [1, 1, 0, 1]],)
Expected
4
Case 2
Input
([[1, 1, 1, 1]],)
Expected
1

Browse More Questions

Show the approach

Approach

For each cell, track the side length of the largest all-one square ending at that cell as its bottom-right corner. A zero gives side length zero. A one extends the minimum of the lengths immediately above, to the left, and diagonally above-left. These three smaller squares, together with the current cell, cover the proposed larger square. Conversely, any larger square ending at the current cell would require all three neighbors to support its smaller sub-squares. Use zero for neighbors outside the matrix. While updating one array left to right, its current slot holds the prior row and its preceding slot holds the current row. Save the old current slot for the next cell’s diagonal. Squaring the largest side length gives the area.

Time complexity:
O(rows × columns)
Space complexity:
O(columns) auxiliary space