Pack Orders into Delivery Boxes

Hard ⏱ 18 min 39% acceptance ★★★★★ 4.9
Write pack(weights, box_limit) assigning items (kg) to boxes using first-fit: place each item into the first existing box it fits, else open a new box. Return the list of boxes (each a list of weights). Items above the limit raise ValueError. (First-fit isn't optimal — say so — but it's the interview standard.)

Examples

Example 1
Input
pack([4, 8, 1, 4, 3], 10)
Output
[[4, 1, 4], [8], [3]]
Explanation

Each item drops into the first box with room.

Constraints

  • First-fit order preserved.
  • Oversized items raise ValueError.

Topics

Business Logicgreedy packing

Companies

AmazonDelhiveryBlueDart

Hints

Hint 1

Track remaining capacity per box.

Hint 2

Sum the box to test fit, or keep a parallel totals list.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.