The Put Boxes Into the Warehouse I problem gives you boxes and warehouse rooms, both represented by their heights. Boxes can only be pushed from one end (left) and must fit under the room height at each position. Find the maximum number of boxes you can push into the warehouse. This coding problem uses a greedy approach after sorting. The array, sorting, and greedy interview pattern is the core.
Pinterest and Google ask this because it tests greedy sorting with a monotone height constraint. The key insight is to sort boxes in descending order and precompute the "effective ceiling" of each room (the minimum height seen so far from the left), then greedily fit the largest boxes first.
Sort + precompute effective ceiling + greedy matching. Sort boxes descending. Compute minHeight[i] = min of warehouse[0..i] (effective height limit at position i). Sort effective ceilings descending as well. Greedily match each box to the first room it fits (two-pointer approach).
boxes=[5,3,1], warehouse=[3,4,1,2]. Effective ceilings: [3,3,1,1] (min prefix). Sort boxes desc: [5,3,1]. Sort ceilings desc: [3,3,1,1].
Warehouse packing problems use the "effective constraint" precomputation: the actual usable height at position i is min(heights[0..i]). This transforms a 2D matching problem into a 1D greedy. Practice similar "fit items into containers with monotone constraints" problems. The descending sort + greedy match pattern generalizes to interval scheduling, bin packing, and capacity planning problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Put Boxes Into the Warehouse II | Medium | Solve | |
| Minimize Product Sum of Two Arrays | Medium | Solve | |
| Destroying Asteroids | Medium | Solve | |
| Minimum Cost to Make Arrays Identical | Medium | Solve | |
| Partition Array Such That Maximum Difference Is K | Medium | Solve |