Largest square from 1x1 and 2x2 tiles
I was asked this
No reports yet. Sign in to say where you were asked it.
You have M small tiles of size 1×1 and N large tiles of size 2×2. Build the largest possible square: every cell of the square must be covered by exactly one tile, with no gaps, no overlaps, and no tile sticking out past the edge. You do not have to use all of your tiles.
Implement:
def max_square_side(m: int, n: int) -> int: ...
Return the side length of the largest square you can build, or 0 if you cannot build any square.
Constraints
0 <= M, N <= 1_000_000_000- Aim for better than linear time in
M + N.
Example 1
M = 5, N = 1 returns 3. Put the 2×2 tile in one corner of a 3×3 square and fill the remaining 5 cells with 1×1 tiles.
Example 2
M = 0, N = 3 returns 2. The tiles have total area 12, but three 2×2 tiles cannot fill a 3×3 square (area 9 is not a multiple of 4) and a 4×4 square needs four of them. One 2×2 tile alone makes a 2×2 square.
Example 3
M = 0, N = 0 returns 0.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
