Code RoomPack into boxes
MediumPrep Room Coding #364

Pack into boxes

CodingAlgorithms & data structuresEntry–Mid~14 min

A packing station fills shipping boxes from a line of items that must stay in order. Each box holds at most `capacity` weight units. Work through the item weights left to right, adding each item to the current box; when an item would push the box over capacity, seal the box and start a new one with that item. No single item exceeds the capacity. Return how many boxes get used (0 for no items). Example: weights [3, 4, 5, 2] with capacity 7 use 2 boxes.

Implement
boxes_needed(weights: list[int], capacity: int) → int
Examples
in[[3,4,5,2],7]out2
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 14 min
InputExpectedGot
[[3,4,5,2],7]2not run yetsample