Largest Square Ad Placement in a Grid of Free Pixels
Last updated: November 29, 2026
Quick Overview
A binary matrix marks which cells of a feed canvas are free (1) or occupied (0). Return the area of the largest square made entirely of free cells, where an ad unit could be placed.
Meta
November 29, 20260
0
0 solved
A binary matrix marks which cells of a feed canvas are free (1) or occupied (0). Return the area of the largest square made entirely of free cells, where an ad unit could be placed.
Shows up in Meta onsite coding rounds with an ads or layout flavor. The interviewer wants the min-of-three recurrence with a clear argument for why it is correct, not just recall, and expects a one-row space optimization.
What the Interviewer Expects
- Define dp as the side of the largest square ending at a cell
- Justify the min of top, left and diagonal plus one
- Track the running maximum side
- Return area, not side
- Optimize to one row with a saved diagonal
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you count all square submatrices of free cells instead?
- How would you find the largest rectangle rather than square?
- What if the canvas updates one cell at a time and you must answer after each update?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is an r x c matrix of 0s and 1s (may be empty). Return the area of the largest all-ones axis-aligned square. If there are no free cells, return 0....
High-Level Design
Let side[i][j] be the side length of the largest all-ones square whose bottom-right corner is (i, j). If the cell is 0, that is 0. If it is 1, the square can extend only as far as all three neighb...