Fixing a bug with an AI assistant, step by step
Stage 1 of the Bakery Order Service problem, worked through the way you'd do it in an AI-allowed interview: reproduce the bug, catch the assistant's plausible wrong fix, fix the cause, and explain it.
What's real here: the code changes and the terminal output are what you get in the starter repo. The assistant's replies are ours, written to show how assistants often answer, and shortened. Yours will word things differently, and may get different things wrong.
Step 1 Read the whole ticket
Stage 1 is a bug report from the shop manager:
From: Marta (shop manager)
Since we took the pumpkin pie off the menu, our croissant count keeps drifting. This morning a customer tried to order 4 croissants and a pumpkin pie. The site correctly said the pie isn't available any more, and no order was created, but our croissant stock went from 20 to 16, and the 3 pies we still have in the back now show as 2. I think it happens with other rejected orders too, not just the pie.
Two things to take from it before you open any code. The reported case: four croissants and a pumpkin pie that's off the menu, the order is rejected, and croissant stock still drops by 4. And the last line: "I think it happens with other rejected orders too." That's a hint that the pie is one example of something more general. Hold on to it. It matters at step 4.
Step 2 Reproduce it before you prompt
In the browser workspace, scratch.py already places the order from the ticket. (Working
locally? It only takes a few lines: place the order in a try block and print the croissant stock
before and after.)
croissants before: 20 order rejected: Pumpkin pie is no longer on the menu croissants after: 16
Then run the stage's tests, so you know what passing looks like:
ERROR: test_next_customer_can_buy_everything_after_a_rejection FAIL: test_discontinued_item_listed_first FAIL: test_mix_of_rejections_then_success FAIL: test_over_the_per_order_limit FAIL: test_the_reported_case_croissants_and_a_discontinued_pie Ran 7 tests FAILED (failures=4, errors=1)
Five of seven fail. Skim the test names: they already tell you more than the ticket does. One is about a per-order limit, and the ticket doesn't mention one.
Two minutes here gives you a quick way to check any fix, yours or the assistant's. Without it, you're taking the assistant's word that it worked.
Step 3 The tempting prompt
Here's the prompt most people type first:
Fix BUG-112: when an order is rejected because a product is off the menu, stock for the other items
still goes down. scratch.py reproduces it.
place_order doesn't check whether a product is on the menu until pricing, and by then the
stock has been reserved. The simplest fix is to check on_menu in
_collect_quantities, which already validates each line before anything is reserved:
--- a/bakery/orders.py+++ b/bakery/orders.py@@ -8,5 +8,5 @@ from bakery.catalog import Catalog, CustomerDirectory-from bakery.errors import EmptyOrderError, InvalidQuantityError, OutOfStockError+from bakery.errors import EmptyOrderError, InvalidQuantityError, OutOfStockError, ProductUnavailableError from bakery.inventory import Inventory from bakery.models import Order@@ -66,5 +66,7 @@ if quantity <= 0: raise InvalidQuantityError(f"quantity for {sku} must be at least 1")- self._catalog.get(sku) # raises UnknownProductError for typos+ product = self._catalog.get(sku) # raises UnknownProductError for typos+ if not product.on_menu:+ raise ProductUnavailableError(f"{product.name} is no longer on the menu") quantities[sku] = quantities.get(sku, 0) + quantity if not quantities:
Now an off-menu product is rejected before any stock is reserved, so the croissant count stays at 20.
It's a reasonable answer to the question it was asked. The prompt described one case, so the assistant fixed one case. Its explanation is half right, and that's what makes it convincing.
Step 4 Check it: the ticket's case passes, one test doesn't
croissants before: 20 order rejected: Pumpkin pie is no longer on the menu croissants after: 20
The case from the ticket is fixed. Now the tests:
FAIL: test_over_the_per_order_limit Ran 7 tests FAILED (failures=1)
This is where people go wrong. It's tempting to call one failure a fluke, or to ask the assistant to "make that test pass." Read the test instead:
def test_over_the_per_order_limit(self):
with self.assertRaises(InvalidQuantityError):
self.place("ana", [("croissant", 2), ("sourdough", 1), ("celebration-cake", 3)])
self.assert_stock_untouched()
A celebration cake can only be ordered two at a time, and that limit is checked in pricing too, after
the stock is reserved. It's one of the "other rejected orders" from the ticket. The assistant fixed the
symptom it was shown, not the cause. Its change also copies the "off the menu" check out of
pricing.py, so that rule now lives in two places.
Here the tests caught it. In a real round, the tests you're given may not cover the neighbouring case. Asking "what else takes this code path?" is how you find it when they don't.
Step 5 Undo it, and ask for a map instead of a fix
Undo the change (in the workspace, the assistant's answer has an Undo button). Then ask a question whose answer you can check:
Don't change anything yet. List every place place_order can reject an order, in the order
they happen, following the calls into the other modules. For each one, say whether stock has already
been reserved at that point.
self._customers.getraisesUnknownCustomerError. Nothing is reserved yet._collect_quantitiesraisesInvalidQuantityError(a quantity below 1),UnknownProductError(no such SKU) orEmptyOrderError. Nothing is reserved yet._reserve_stockraisesOutOfStockError. It releases the lines it reserved before the one that failed, so nothing is left reserved.self._pricing.quotecalls_price_line, which raisesProductUnavailableError(off the menu) andInvalidQuantityError(more thanmax_per_order). Stock is already reserved here, and nothing releases it.self._repository.addraisesValueErrorif the order id already exists. Stock is reserved here too.
That's what you needed: two checks in pricing, both after the reservation. Take 30 seconds to open
pricing.py and see the two raise lines yourself. Until you've looked, the list
is the assistant's claim, not a fact. (Item 5 comes back in step 8.)
Step 6 Pick the fix yourself, then ask for exactly that
There are two ways to fix it: release the stock when pricing raises, or price the order before reserving
anything. Before you choose, check that pricing is safe to run first. quote reads the
catalog, builds the lines and returns a Quote. It doesn't change anything.
In place_order, call self._pricing.quote before self._reserve_stock,
with a short comment saying why. Change nothing else.
--- a/bakery/orders.py+++ b/bakery/orders.py@@ -44,6 +44,8 @@ quantities = self._collect_quantities(items) + # Pricing can still reject the order (off the menu, over the per-order limit),+ # so it runs before anything touches stock.+ quote = self._pricing.quote(customer, quantities) self._reserve_stock(quantities)- quote = self._pricing.quote(customer, quantities) order = Order(
A narrow prompt gets a narrow change: one line moved and a comment. You can read the whole diff in a few seconds and explain every line of it.
Step 7 Verify it
The stage's tests:
Ran 7 tests OK
The tests for what already worked, because you changed the main code path:
Ran 23 tests OK
And the case from the ticket:
croissants before: 20 order rejected: Pumpkin pie is no longer on the menu croissants after: 20
Run the old tests every time, not just the new ones. Here the fix reorders the main path through the codebase, and that's exactly the kind of change that breaks something you weren't looking at.
Step 8 Say what the tests don't cover
Remember item 5 of the map: self._repository.add runs after stock is reserved. In this
codebase it only fails on a duplicate order id, which the id counter shouldn't produce. But it stands in
for a real database write, and those do fail. None of the tests you were given cover it.
Then let the interviewer choose. Either answer is fine. What counts is that you found the gap and could size it.
It's worth the minute because some interviewers have tests you haven't seen, and run them once you say you're done. This problem has some too. Its hidden tests for stage 1, on the fix from step 6:
FAIL: test_a_failed_save_puts_the_stock_back Ran 2 tests FAILED (failures=1)
That's the write failing. Because you said it first, the failure is a gap you'd already named, not one the interviewer found. (The reference solution wraps the write and passes. In the workspace, the hidden tests are under Hidden tests in the Run menu.)
Step 9 Explain it in 30 seconds
Before moving on, sum the stage up. Something like:
place_order reserved stock and then priced the order,
and pricing can still reject it, for an off-menu product or too many of a limited item. Nothing put the
stock back. Pricing has no side effects, so I moved it before the reservation, and now every check runs
before we touch stock. The assistant's first fix only covered the off-menu case. The per-order-limit test
caught that, so I had it list every rejection point before I chose the fix."
It names the assistant's mistake, and it should. In an AI-allowed round, the interviewer wants to see you catch the assistant being wrong. Hiding it throws away your best evidence.
Step 10 What one-shotting it would look like
The other way to start is a single prompt:
Here's the README. Do all four stages.
The reference solution for all four stages and the change request adds about 200 lines across 7 files, so expect an answer that size. It might even pass the tests. Then the interviewer asks:
- "Why does pricing run before the stock is reserved now?"
- "What happens if storing the order fails?"
- "Why does the coupon come off before the loyalty discount?"
- "Marketing wants coupons for one category. Where does that go?"
To answer, you have to read and understand well over a hundred lines you didn't write, while they watch. And they never saw how you think, which is what the round is grading. Steps 1 to 9 took longer than one prompt, but every one of them was something the interviewer could see.
There are times when a bigger generation is fine, like boilerplate you've already planned. Our guide covers when: Should you let the AI write the whole thing?
Your turn
Stage 2 adds coupon codes, and it's a bigger change: new rules, a new error and new fields on
Order. Work it the same way. Read the README section and the code it touches, ask
the assistant for a plan or a map before any code, and keep each change small enough to explain.
Open it in the browser workspace Download the starter repo
- The Bakery Order Service problem: every stage, a self-check rubric and a walkthrough of each one
- Our guide to AI-assisted coding interviews: how the rounds work and what interviewers grade
- All the AI coding interview practice problems
Reviewed by Stephen Cognetta, CEO of Aced. Last updated: December 5, 2026
Interview Cake