Determine whether squares can fit
ByteDanceSite Reliability Engineer · Technical ScreenLast updated
This question evaluates spatial reasoning, combinatorial packing, and constraint-satisfaction skills in the context of placing integer-aligned squares within an m x n rectangle.
You are given an m x n rectangular grid and an array squares, where each value represents the side length of a square that must be cut from the original rectangle.
Determine whether it is possible to cut out one square of each requested size such that:
- every square is fully contained inside the
m x nrectangle, - squares do not overlap,
- cuts follow integer grid lines,
- unused leftover area is allowed.
Assume the number of requested squares is small enough for a search/backtracking solution.
Examples:
m = 30,n = 40,squares = [30, 10]->truem = 30,n = 40,squares = [30, 20]->false
Return true if all requested squares can be cut from the rectangle; otherwise return false.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.