HardMathBinary SearchGreedyUpdated Sep 2026

1739. Building Boxes

Asked at Adobe

Difficulty

Hard

Time

O(k) where k is the pyramid side

Space

O(1)

Pattern

Math

Companies

1

Problem

You have a cubic room whose width, length and height are all n units. You must place exactly n unit cubes inside it, following two rules.

First, every cube must rest on the floor or on top of another cube. Second, if a cube sits on top of another cube, then all four vertical sides of the lower cube must be blocked — each side must touch either a wall or another cube. In other words, a cube can only carry a cube above it when it is fully surrounded on all four sides.

Return the smallest number of cubes that can touch the floor in a valid arrangement of n cubes.

Cubes can only stack when fully surrounded

floor1112The corner cube (orange) is walled on two sides andblocked by two cubes, so a cube can sit on itn = 4 cubes placedfloor cubes = 31 cube lifted on top
A cube may carry another cube only if all four of its vertical sides touch a wall or another cube.

Constraints

  • ●1 <= n <= 10^9

Examples

Example 1
Inputn = 3
Output3
WhyThree cubes can sit side by side on the floor in a corner, so all three touch the floor. No cube can be lifted because a lifted cube would need a fully surrounded cube underneath, and with only 3 cubes that is impossible.
Example 2
Inputn = 4
Output3
WhyPlace a 2-by-2 block of cubes in a corner. The cube in the corner is surrounded by a wall on two sides and by the other two cubes on the other two sides, so a fourth cube can sit on top of it. Only the 3 cubes on the floor count.
Example 3
Inputn = 10
Output6
WhyA stair pyramid with base triangle of side 3 holds exactly 1 + 3 + 6 = 10 cubes, and its base has 6 cubes. That is the most efficient shape, so 6 is the minimum.

Asked At

1 companies
CompanyDifficulty
AdobeHardView all Adobe questions →

How to Think About It

1

Brute force: for every candidate ground count, stack cubes upward and check capacity. Infeasible because n reaches 10^9 - you cannot simulate cube by cube. The answer must come from closed-form geometry.

2

Key insight: the most space-efficient building is a stair pyramid. A full pyramid whose base triangle has side s holds s(s+1)(s+2)/6 cubes total, of which s(s+1)/2 touch the ground.

3

First, binary search the largest s such that s(s+1)(s+2)/6 <= n. Those cubes are already supported. The leftover cubes must be added as a new bottom row extending the base triangle to side s+1.

4

The remainder rule: the j-th ground cube added to the extended triangle row brings j additional cube positions above it (itself plus j-1 stacked). Greedily accumulate 1, 2, 3, ... until the total added covers the remainder.

5

Check against known cases: n=3 gives s=2, total 3, remainder 0, answer 3; n=10 gives s=3, total 10, remainder 0, answer 6; n=4 gives s=2, total 4, remainder 0, answer 3.

Optimal Approach

A ground cube supports a whole stack, so the cheapest layout is a stair pyramid. Binary-search the largest s whose total volume s(s+1)(s+2)/6 is at most n, and record its ground count s(s+1)/2 plus used cubes. Then handle the remainder r: keep adding the next ground cube of the extended triangle row, where the j-th cube adds j more cube positions, until the accumulated total reaches r.

Walkthrough for n = 4:

  1. Try s=1: 123/6 = 1 <= 4. Try s=2: 234/6 = 4 <= 4. Try s=3: 3*4*5/6 = 10 > 4. So s=2.
  2. Used = 4, remainder = 0, ground so far = 2*3/2 = 3.
  3. Answer = 3.

Time: O(k) where k is the pyramid side, capped near 2000 for n <= 10^9 (effectively constant); space: O(1).

Stair pyramid plus a greedy remainder row

pyramid s=3111110100Base triangle of side 3 holds 6 ground cubes and 10cubes totalbinary search s: s(s+1)(s+2)/6 <= nground = s(s+1)/2, used = pyramid totalremainder r = n − usedadd ground cubes: j-th adds j cubesThe j-th new ground cube in the extended row supportsj cubes above itn=10 → s=3, used=10, r=0answer = 6n=15 → s=3, used=10, r=5add 1+2+3=6 >= 5 → +3 groundanswer = 6 + 3 = 9
Binary-search the biggest full pyramid that fits, then add ground cubes one by one until the leftover cubes are used up.
ComplexityTime O(k) where k is the pyramid sideSpace O(1)The binary search over the pyramid side is O(log 2000) and the remainder loop adds at most about 2000 ground cubes, so the total work is tiny.

What Trips People Up in Real Interviews

1

Simulating cube by cube. With n up to a billion any loop touching each cube individually times out. The pyramid formulas compress everything into O(1)-class math.

2

Swapping the two formulas. The ground layer of a side-s triangle is s(s+1)/2; the full pyramid is s(s+1)(s+2)/6. Flipping them corrupts the binary search threshold and the final ground count.

3

Forgetting the second phase entirely. Finding the largest full pyramid is only half the problem - leftover cubes still demand new ground boxes. Skipping the greedy remainder step under-reports the answer.

4

Off-by-one in the binary search. You want the largest s whose pyramid fits (<= n). Use the seek-largest form mid = (lo + hi + 1) / 2 and move hi down when the candidate overflows.

5

Integer overflow in s(s+1)(s+2). The product reaches the 10^9 range, so compute in a 64-bit type (natural in Python, but C++ and Java need long) before dividing by 6.

Solution Code

O(k) where k is the pyramid side time
def minimumBoxes(n):
    lo, hi = 0, 2000
    while lo < hi:
        mid = (lo + hi + 1) // 2
        if mid * (mid + 1) * (mid + 2) // 6 <= n:
            lo = mid
        else:
            hi = mid - 1
    s = lo
    total = s * (s + 1) * (s + 2) // 6
    answer = s * (s + 1) // 2
    remaining = n - total
    added = 0
    row = 0
    while added < remaining:
        row += 1
        answer += 1
        added += row
    return answer

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Building Boxes problem?

With n unit cubes, build a house where every cube above the ground layer is supported by a cube directly beneath it, minimizing the number of cubes that touch the ground. This greedy-with-binary-search hard problem is a signature Adobe math question with n up to one billion.

How do you solve Building Boxes?

The optimal approach runs in O(k) where k is the pyramid side time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Building Boxes?

Building Boxes is asked at Adobe. It is a hard difficulty problem.

What are common mistakes on Building Boxes?
  • Simulating cube by cube. With n up to a billion any loop touching each cube individually times out. The pyramid formulas compress everything into `O(1)`-class math.
  • Swapping the two formulas. The ground layer of a side-s triangle is `s(s+1)/2`; the full pyramid is `s(s+1)(s+2)/6`. Flipping them corrupts the binary search threshold and the final ground count.
  • Forgetting the second phase entirely. Finding the largest full pyramid is only half the problem - leftover cubes still demand new ground boxes. Skipping the greedy remainder step under-reports the answer.
  • Off-by-one in the binary search. You want the largest s whose pyramid fits (`<= n`). Use the seek-largest form `mid = (lo + hi + 1) / 2` and move hi down when the candidate overflows.
  • Integer overflow in `s(s+1)(s+2)`. The product reaches the 10^9 range, so compute in a 64-bit type (natural in Python, but C++ and Java need `long`) before dividing by 6.