Practice problem · Hard

Soft-Deadline Bin Packing

Pack items into a sequence of indexed bins with fixed capacities. One stage in Python with an AI assistant. Pro.

The problem

Background. Pack items into a sequence of indexed bins with fixed capacities. Each item has a size, a value (earned only if it is packed on time), and a deadline (the highest bin index that counts as "on time"). Packing an item after its deadline is allowed but incurs a late penalty proportional to its value; dropping an item (not assigning it to any bin) incurs a drop penalty proportional to its value. The goal is to maximize the overall score balancing on-time rewards, late penalties, and drop penalties.

Implement the solve(instance) function in solution.py.

INPUT FORMAT

The function receives a single argument instance, a dataclass with fields:

  • name: str (optional identifier)
  • bin_capacities: List[int] — list of positive integers, length B (bins indexed 0..B-1)
  • items: List[Item] — list of Item objects in input order. Each Item has fields:
    • id: int (canonical id assigned 0..N-1 in input order)
    • size: int (positive)
    • value: int (positive)
    • deadline: int (non-negative; an item placed into any bin with index <= deadline is on-time)
  • late_penalty_factor: float (>= 0)
  • drop_penalty_factor: float (>= 0)

OUTPUT FORMAT

Return a dict with keys:

  • "bins": List[List[int]] — a list of length B; each element is a list of item ids assigned to that bin. No item id may appear more than once across all bins.
  • "dropped": optional List[int] — explicit list of dropped item ids. Any item not present in any bin and not in the explicit "dropped" list is implicitly treated as dropped. The dropped list, if provided, must be disjoint from all bin lists.

A solution is valid only if no bin's total packed sizes exceed its capacity, and every referenced item id is in range 0..N-1 and appears at most once across bins/dropped.

SCORING

Score = (sum of values of on-time packed items) - late_penalty_factor * (sum of values of late packed items) - drop_penalty_factor * (sum of values of dropped items)

Higher score is better. The two penalty terms (late and drop) are multiplied by the respective instance factors and subtracted. Solutions may trade packing items late vs dropping them depending on the penalty factors.

WORKED EXAMPLE

Input: { "name": "example_small", "bin_capacities": [10, 8, 5], "items": [ {"size": 2, "value": 5, "deadline": 0}, {"size": 5, "value": 8, "deadline": 1}, {"size": 4, "value": 6, "deadline": 2}, {"size": 6, "value": 10, "deadline": 0} ], "late_penalty_factor": 1.5, "drop_penalty_factor": 2.0 }

A valid return value (one possible packing): { "bins": [[0, 3], [1], [2]], "dropped": [] }

Explanation: bin 0 holds items 0 and 3 (sizes 2+6 <= 10) and both are on-time (index 0 <= their deadlines). Bins 1 and 2 hold items 1 and 2 on-time. No item is late or dropped, so the score is 5 + 8 + 6 + 10 = 29.

HOW SUBMIT JUDGES

  • The second public test first runs solve() on a few tiny hand-built instances, each with a single forced choice, and requires the best possible score on every one.
  • Each scoring test samples 120–150 instances from data/instances.txt, requires every output to be valid, and compares the average score with a threshold: 1300 and 1475 on the two public samples, 1350 and 1620 on the two hidden ones.
  • All instances of a test share its 10-second limit, which also covers loading the data, so budget well under 70 ms per instance.

Done when Submit goes green.

Example 1
Input: {"name":"example_small","bin_capacities":[10,8,5],"items":[{"size":2,"value":5,"deadline":0},{"size":5,"value":8,"deadline":1},{"size":4,"value":6,"deadline":2},{"size":6,"value":10,"deadline":0}],"late_penalty_factor":1.5,"drop_penalty_factor":2.0}
Output: {"bins": [[0, 3], [1], [2]], "dropped": []}

Items 0 and 3 fit in bin 0 on time (2+6<=10). Items 1 and 2 are placed on time in bins 1 and 2. Nothing is late or dropped, so the score is 5+8+6+10 = 29.

Constraints

  • Python 3.12+ standard library only
  • Time limit: 10 seconds per test, shared by all 120–150 instances the test runs (loading the data included)
  • Only solution.py is editable
  • Instances in data/instances.txt have 12–26 bins with capacities 33–397
  • They have 72–234 items, with sizes 1–381 and values 1–960
  • Penalty factors range from about 0.2 to 3

The files you get

  • Yours to edit: solution.py.
  • Read-only: lib.py.
  • Data: data/instances.txt.
  • Tests you can read and run: public_tests/test_stage_2.py, public_tests/test_stage_3.py. Hidden tests run on Submit.

The round: an agent in the editor

The assistant here works like the one in the interview IDE. It reads your files, runs code and proposes changes, and you accept or reject each one. It doesn't volunteer ideas, so what gets built is what you asked for. Along the way the interviewer asks about your decisions, and at the end the report says what you checked and what you accepted unread.

Frequently asked, briefly answered

Is Soft-Deadline Bin Packing free?

It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.

Which company asks something like this?

No single company. It is practice for the format itself: an existing codebase, an assistant in the editor and a bar that rises stage by stage.

Do I bring my own AI tool?

No. The assistant is in the editor, same as in the interview IDEs at Meta, Stripe and Amazon. What you choose at Begin is the format: an agent that proposes changes you accept or reject, an answer-only assistant that can't write code, or no assistant at all.

What does the interviewer ask?

One question at the end about the whole session, your own code and your decisions. Your answer goes into the report.

Start the problem

Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.

Start this problem → Sign in

More like it: every practice problem, the AI coding interview guide.