Largest Two Non-Overlapping Buildable Squares
Updated
Problem statement
A rectangular map is represented by a binary matrix grid. A cell containing 1 is buildable, and a cell containing 0 is not.
Place two axis-aligned square regions such that:
- Both squares have the same positive side length.
- Every cell in both squares contains
1. - The squares share no cell. Touching along an edge or corner is allowed.
Return the maximum common side length. Return 0 if no valid pair exists. Call this the no-pair value.
Function
largestTwoSquares(grid: int[][]) → intExamples
Example 1
grid = [[1,1,0,1,1],[1,1,0,1,1]]return = 2The left block and the right block are each a 2 x 2 square of buildable cells. A column of unbuildable cells sits between them, so they share no cell. No bigger square fits in a grid this short, so that side length is the answer.
Example 2
grid = [[1,1],[1,1]]return = 1Any two different cells give two squares of side 1 that share no cell. A square of side 2 would cover the whole grid and leave no room for a second one. So the smaller side is the best you can do.
Example 3
grid = [[1]]return = 0The grid has a single cell, so only one square can be placed. A second square has nowhere to go, so the answer is the no-pair value.
Constraints
1 <= grid.length <= 7001 <= grid[i].length <= 700- Every row has the same length.
grid[i][j]is either0or1.
More American Express interview questions
- Maximum Points in a Perimeter-Bounded RectangleOA · Seen Sep 2026
- Minimum Moves to Spread Stones Across a GridOA · Seen Sep 2026
- Maximum Sum of Two Sign-Flipping WindowsOA · Seen Aug 2026
- Crop Text at a Whole-Word BoundaryOA · Seen Aug 2026
- Minimum Parking Roof LengthOA · Seen Aug 2026
- Minimum Pile Reduction StepsOA · Seen Aug 2026
- Disjoint Target-Sum TripletsPHONE SCREEN · Seen Mar 2025
Similar questions at other companies
- Minimum Starting Points to Cross a GridNutanix · OANutanix interview questions
- Minimum Grid InconvenienceAmazon · OA
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Safest Shortest Path Through a GridSnowflake · OASnowflake interview questions
- Maximum Non-Decreasing Array LengthVisa · OAVisa interview questions
- Minimum Starting Health in a GridIntuit · OAIntuit interview questions
- Maximize Array Beauty After DeletionsPoint72 · OAPoint72 interview questions
- Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
- Min Elements to Remove to Make Almost Sorted ArraySalesforce · OASalesforce interview questions