cswork / libraryOpen practice workspace

Coding questions · ByteDance

Determine whether squares can fit

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 n rectangle,
  • 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] -> true
  • m = 30, n = 40, squares = [30, 20] -> false

Return true if all requested squares can be cut from the rectangle; otherwise return false.

Pack Squares in a Rectangle

Given rectangle width and height and square side lengths, return whether every square can be placed axis-aligned without overlap inside the rectangle.

Hints

  1. Start with a direct data structure representation.
  2. Handle edge cases before the main loop.