FastPrepLargest Two Non-Overlapping Buildable Squares
Problems/Largest Two Non-Overlapping Buildable Squares

Largest Two Non-Overlapping Buildable Squares

American Express logoAmerican Express● HardINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an American Express intern online assessment. We kept the rules from the report: the two squares are the same size, all cells are true, and they do not overlap. We also kept the grid limits it gave. We added what to return when there is no such pair, which was the only important gap. Our version is about 98% the same as the reported question.

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[][]) → int

Examples

Example 1

grid = [[1,1,0,1,1],[1,1,0,1,1]]return = 2
grid
01234011011111011

The 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 = 1
grid
01011111

Any 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 = 0
grid
001

The 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 <= 700
  • 1 <= grid[i].length <= 700
  • Every row has the same length.
  • grid[i][j] is either 0 or 1.

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
All American Express interview questionsSee American Express hiring insights
Saved
class Solution {
    public int largestTwoSquares(int[][] grid) {
        // Write your solution here.
    }
}
Checking account…