AiLearn AI Coding
easyStructured · sandbox45 min Premium

Inventory Packer

Pack items with weights into bins with capacities, greedy bin packing with a bug → feature → scale arc.

Greedy / Packing

Problem

A warehouse system packs Item { id, weight } objects into Bin { id, capacity, items }. The starter code has a bug.

Phase 1 — Fix the bug

packSingle(item, bin) currently accepts an item whose weight exceeds the bin's remaining capacity. It must reject it with an OverflowError.

Phase 2 — Feature

Add packAll(items, capacity): pack every item into bins of the given capacity using first-fit, opening a new bin only when no existing bin fits. Return the list of bins. You should aim to minimize the number of bins used.

Phase 3 — Scale

Inputs grow to tens of thousands of items. Sorting before packing helps, but your packing loop itself must not be O(n²) over items × bins in the common case.

What interviewers watch

Whether you justify the greedy rule and the sort order, and whether you verify that packed totals equal input totals — the classic silent bug.