Find the Largest All-One Square in a Binary Matrix
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
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
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
- A square occupies the same number of consecutive rows and columns.