AI Interviews · Question

Final-loop coding: one round builds something from scratch; the other hands you existing code with issues to refactor and fix under time pressure.

Practical coding question reported at OpenAI (2026). Sources, confidence and an answer card.

Researched · updated

eng-122 AI Engineer Classic DSA & object-oriented design Practical coding Low confidence
On this page

What they’re really asking

If you only remember one thing, say this in the first minute: “Before I move any code, I’ll pin what it does today with a test that records its current answers, bugs included. Then I’ll refactor in small steps, running that test after each one, without changing behaviour. Only then will I fix the bugs, one at a time, each with its own failing test, and I’ll say why each change to the recorded answers is intended. If time is short, I’ll do the pin, the few changes that make the bugs testable, and the fixes, and list the rest.”

The rest of this page explains each part of that answer.

The interviewer wants to know whether you can improve code you were handed without breaking it, and show that you didn’t.

That is harder than it sounds, because the round asks for two different things at once. Refactoring means changing the structure of code without changing what it does: splitting a long function, renaming, moving logic. Fixing means changing what it does. If you do both in one step and a result changes, nobody can tell whether you fixed a bug or introduced one. A backend engineer knows this from production: you don’t ship a schema migration and a behaviour change in the same deploy, because when something breaks you can’t tell which one did it.

So the whole answer is about keeping those two kinds of change apart, and having a test that proves which kind each change was.

(The evidence for this round is thin: Glassdoor search snippets describe an OpenAI final loop with one build-from-scratch round and one round of existing code to refactor and fix under time pressure. One candidate said interviewers cared more about structure and tests than big-O Glassdoorsnippet only · 2026. Confidence is low, but the skill is standard.)

Clarify first

Ask, and state an assumption for each:

  • What counts as correct? The existing tests, a spec, or “what it does now, minus the bugs”? “I’ll treat today’s behaviour as correct unless you tell me it’s a bug.”
  • Are the bugs known or mine to find? A list changes how you spend the time.
  • Is the public interface fixed? “I’ll assume yes, since other code calls it.”
  • Language, test runner and dependencies? “I’ll assume the standard library only.”
  • How much time for refactoring versus fixing? Ask, then plan to show both.

Answer

Words you need first

  • Characterization test (also called a golden master): a test that runs the current code over many inputs, records its answers, and fails if any answer changes. It doesn’t say the answers are right. It says they haven’t moved.
  • Seam: a place where the code can be split or where a dependency can be swapped, such as the line between “work out the price” and “save the order”.
  • Injecting a dependency: passing the clock, the database or the random generator in as a parameter instead of calling it directly, so a test can supply a fake one.

The example: a pricing function you were handed

Imagine you get this function, with no tests:

legacy_price(items, coupon)
   1. sum qty × price over the items
   2. if coupon == "TEN": take 10% off
   3. if total > 100: take 5 off
   4. round to 2 decimal places

It works, mostly. But it mixes three jobs (summing, applying discounts and rounding), and some behaviour is unclear from reading it alone. Should the 5-off rule be checked before or after the coupon? Should an unknown coupon such as "BOGUS" be ignored silently or rejected? Those are questions for the interviewer, not decisions to make quietly. Here is how the four steps play out on it.

StepWhat you doWhat to say in the interview
1. PinRecord today’s answers over 500 generated orders.”This test doesn’t say the answers are right, only that they haven’t moved.”
2. RefactorSplit it into subtotal, apply_coupon, apply_threshold and round. Run the pin after each split.”Pure restructuring. The pin stays green or I undo the step.”
3. FixFor each agreed bug: a failing test, the fix, then update the pinned answers it changes, on purpose.”This fix changes the pinned answers for coupon orders near 100, and here’s why that’s intended.”
4. ExplainName the pieces, their rules, and which test protects which behaviour.”If someone changes the threshold rule, this test catches it.”

Step 1: pin current behaviour

Generate many inputs, record what the code returns today (bugs included), and assert that the refactored code returns the same:

def cases(n=500, seed=1):
    rng = random.Random(seed)
    for _ in range(n):
        items = [{"qty": rng.randint(0, 5), "price": rng.choice([0.99, 9.5, 20, 49.99])}
                 for _ in range(rng.randint(0, 4))]
        yield items, rng.choice([None, "TEN", "BOGUS"])

def test_golden_master():
    golden = [legacy_price(i, c) for i, c in cases()]   # today's behaviour, bugs included
    assert [new_price(i, c) for i, c in cases()] == golden

The fixed seed means the same 500 orders every run, so a failure is repeatable. The inputs deliberately include empty orders, zero quantities, all three coupon values and prices that don’t add up neatly in floating point, because those are where behaviour tends to differ.

Step 2: refactor in small, green steps

Split the code along the seams it already has: parsing input, computing, and side effects such as saving or logging. Inject what makes testing hard: the clock, I/O and randomness. Rename for meaning. Run the golden test after each step. Don’t change behaviour yet, even when you can see a bug.

Say it like this: “I can see what looks like a bug in the discount order, but I’m not touching it yet. I’ll write it down and fix it after the structure is safe.”

Step 3: fix bugs one at a time

For each bug, write the failing test, then fix it. Then update the golden values on purpose for the cases the fix changes, and say why. Mixing refactoring and fixing makes a refactoring mistake look like a fix.

Step 4: explain the structure you left

Name the pieces, the rules each one keeps, and which tests protect which behaviour. If the snippet’s emphasis is right (Glassdoorsnippet only · 2026), this short summary is the part that gets graded.

Under time pressure

Do the golden test, plus one or two extractions that make the bugs testable. Fix the bugs. List the refactors you would do next. A full rewrite you can’t verify in time is the worst outcome, because you end the round with code that may be nicer and that nobody can show still works.

Senior vs Staff

Refactor and fix

Senior

  • Pins behaviour, refactors in green steps, fixes each bug with a test and explains it.

Staff

  • Chooses the seams that make the next change cheap, stops refactoring when the bugs are testable, and states what they deliberately left alone and why.

In plain words: Senior does the steps in the right order. Staff also decides how much refactoring is enough, and says out loud what they chose not to touch.

Small-team version

Coding agents are good at refactors, and those refactors are easy to approve, because the diff reads well. That is the risk. A “pure refactor” from an agent can quietly change behaviour in a corner case, and nobody notices until a customer does.

The habit that catches it:

  • Ask the agent for the characterization test before the refactor.
  • Run it yourself on the old code, then on the new code.
  • Keep it in the suite afterwards, so the next refactor is checked too.

The cost is small. The 500-case golden test above runs in well under a second, and the agent session that drafts it costs a few dollars at list prices. Skip it only where the code has no callers yet. Add it before the first refactor of anything in production.

Pitfalls

  • Starting with a rewrite. You lose the old behaviour as a reference, so you can’t prove the new code matches it.
  • Refactoring with no test that pins behaviour. Every green run then proves nothing about what changed.
  • “Fixing” a behaviour that a caller depends on, without asking. A bug to you may be a feature to someone else’s code.
  • Spending the round on big-O when the code’s problem is structure. The reported emphasis is structure and tests.

Go deeper

Family