As reported: American Express on-campus online assessment for a summer internship (2026): the second of three coding questions; the grid can be up to 700 × 700.
You are given an N × M grid of booleans. A cell is true when a solar plant can be built on it. You want to build two square solar plants of the same size:
- every cell inside each square must be
true; - the two squares must not share any cell;
- a
1 × 1square is a valid plant.
Return the largest possible side length of the two squares, or 0 if two squares cannot be placed.
Example 1
1 1 1 0
1 1 1 0 -> 2
1 1 1 1
0 0 1 1
One 2 × 2 square covers rows 0–1, columns 0–1; the other covers rows 2–3, columns 2–3. Two 3 × 3 squares do not fit.
Example 2
1 0 1
0 1 0 -> 1
1 0 1
No 2 × 2 block is all true, but any two true cells are two 1 × 1 plants.
A close relative is LeetCode 221 — Maximal Square, which asks for one square instead of two.
Constraints: 1 ≤ N, M ≤ 700.
Code runner format: one line: the grid as a JSON array of N strings of length M, '1' for true and '0' for false (example 1 is ["1110","1110","1111","0011"]). Implement maxTwoSquares(grid); the harness prints the returned number.