# Re-Engineering Round: Fix Bugs in Unfamiliar Code

> How to find and fix a bug in code you have never seen: reproduce it, find the cause, fix it once, and prove it stays fixed, for your AI interview.

**In short:** Don't start by editing. Reproduce the bug first, find its cause with evidence, make the smallest fix, then add a test so the bug can't return quietly. Each step is one you can say out loud.

AI and LLM interview question Q401, Forward Deployed Engineering and applied AI. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/re-engineering-round-fix-bugs-in-unfamiliar-code

## Say it in 30 seconds

In this round you get code I have never seen and a bug report. I don't start by editing. First I reproduce the bug, so I can watch it fail. Then I find the cause with evidence, using a debugger or a log line, not a guess. I make the smallest fix in the right place, check the other places that call it, and add a test that fails before the fix and passes after. I say each step out loud as I go.

## Say it in 2 minutes

This kind of round may give you a small codebase you have never seen, a bug report, and a time limit. Plan on not knowing the code already, and let them see how you work. Take a claims service at a mid-size insurer. Support reports that claim CL-1001 shows a repair quote that belongs to CL-1002. I start by reproducing it: two calls, and the same bug appears. Now I know the bug is real, and I have a way to check my fix.

Next I find the cause, not the symptom. I read the function that attaches files and see an empty list as a default argument. Python creates that default once, when the function is defined, so every call that leaves the argument out shares one list. I confirm it with a print that shows both claims pointing at the same list object. Then I change the default to None and create a fresh list inside the function. The smallest change in the right place is easier to explain and easier to trust.

After that I search for other functions with the same pattern, and I add a test that fails on the old code and passes on the new one. I run the whole test set, not only the new test. Finally I tell the interviewer what I changed, why, and what I did not check. Being clear about the gaps matters as much as the fix.

The method is the same in any codebase: read the report, reproduce it, narrow it down, fix it once, and prove it. I practise on small broken projects until the steps feel automatic.

## What is a re-engineering round, in plain words?

In this kind of round you may get a small codebase you have never seen, a bug report, and a time limit. Plan to show how you work: how you find the bug, how you prove it, and how you fix it without breaking anything else.

Think of a mechanic handed a stranger's car with a strange noise. A good one doesn't swap parts first. They listen, reproduce the noise, check the likely causes, then fix one thing and drive the car again.

In an AI codebase the same steps apply: a retrieval function returns the wrong document for a customer, and you reproduce it with that one question before touching the code.

Ask the recruiter which language and tools you may use. The example below is made up, but the steps are the ones that count.

- **Reproduce:** Make the bug happen again on purpose, with the smallest input you can.
- **Root cause:** The real reason the bug happens, not just the line where it shows up.
- **Stack trace:** The list of calls that led to an error, printed when the code fails.
- **Regression test:** A test that fails on the bug, so the same bug can't come back unnoticed.

## What is the method, step by step?

Do the steps in order. Each one stops you from guessing.

1. **Read the report:** Write down the exact input, the expected result and the actual result. A vague report wastes the round.
2. **Reproduce it:** Run the smallest input that shows the bug. If you can't make it fail, you can't prove a fix.
3. **Find the cause with evidence:** Use a debugger, a log line or the stack trace to see the real values. Change one thing at a time.
4. **Fix the cause once:** Change the code where the mistake is. Keep the change small enough to explain in one sentence.
5. **Check the other callers:** Search for the same pattern elsewhere. One bug often has siblings.
6. **Prove it with a test:** The test fails before the fix and passes after. Then run the whole test set, not only the new test.

*Diagram, From bug report to a proven fix: The main path from a bug report to a proven fix: reproduce it, find the cause with evidence, fix it once, then prove it with a test that fails before and passes after. The written method also checks the other callers before the test.*
1. You turn the report into a run you can repeat.
2. Once the bug fails on cue, you look for its cause.
3. You make one small fix in the place the mistake is.
4. A test that fails before and passes after proves it.

## Which tools find the cause fastest?

If the round uses Python, it ships with its own tools, so you need nothing extra in the room. Start with the error message. Errors that happen while a program runs are called exceptions. The stack trace shows the calls that led to one.

- **A print or a log line:** The fastest check, fine for a small fix. Remove the noise before you finish. The logging guide says debug() is for very detailed output for diagnostic purposes.
- **The debugger, pdb:** Python's standard debugger. It pauses the code at a line you choose and lets you step line by line. In code, breakpoint() opens it at the line where it is called. Inside the pause you can look at the stack and run Python code.
- **The stack trace:** Read it from the bottom: the last line names the error, and the lines above show the calls that led to it.
- **A unit test:** A small check that fails on the bug. The unittest module in the standard library runs these tests.

> **Tip:** Say which tool you chose and why. Values that change across calls, for example, are a good reason to pick the debugger.

## How do you trace a real bug to its cause?

Here is the worked case in plain Python. Two claims call the same function, and the second claim's file appears on the first claim.

```python
# Claims intake at a mid-size insurer. Each claim should get its own attachment list.
def attach(claim_id, file_name, attachments=[]):  # the bug: one list is shared by every call
    attachments.append(file_name)
    return {"claim": claim_id, "attachments": attachments}

def attach_fixed(claim_id, file_name, attachments=None):
    if attachments is None:
        attachments = []  # a new list for each claim
    attachments.append(file_name)
    return {"claim": claim_id, "attachments": attachments}

a = attach("CL-1001", "photo-front.jpg")
b = attach("CL-1002", "repair-quote.pdf")
print("CL-1001:", a["attachments"])
print("CL-1002:", b["attachments"])
print("Same list object:", a["attachments"] is b["attachments"])

c = attach_fixed("CL-1003", "police-report.pdf")
d = attach_fixed("CL-1004", "invoice.pdf")
print("CL-1003:", c["attachments"])
print("CL-1004:", d["attachments"])
```

Output:

```text
CL-1001: ['photo-front.jpg', 'repair-quote.pdf']
CL-1002: ['photo-front.jpg', 'repair-quote.pdf']
Same list object: True
CL-1003: ['police-report.pdf']
CL-1004: ['invoice.pdf']
```

The first three lines of output show the bug: both claims hold both files. The third line, True, means both names point at one list object, which is one list in memory. The cause is the empty list default in the first line of the function, the part that names its inputs. Python makes that default once, so every call that skips the argument adds to the same list.

*Diagram, How one default list leaks between claims: Both claims write into one list, because the default was made once. Each call returns that same list, so claim CL-1001 ends up with the quote from CL-1002.*
1. Claim 1001 calls attach() with its photo.
2. Claim 1002 calls the same function with its quote.
3. Both calls add to one list, made once when the def ran.
4. Claim 1001 now shows the quote from claim 1002.

> "The default value is evaluated only once." (Python tutorial, default argument values)
>
> "This function drops you into the debugger at the call site." (Python standard library, breakpoint())

The fix is in the smallest place: default to None, and make a fresh list inside the function when the argument is None. Then search the codebase for the same pattern, and add a test that calls the function twice and checks that each claim has only its own file. The second half of the script already shows that result.

> **Tip:** Read the function before you change it. Editing first would have hidden the cause.

### How would you fix code that has no tests?

Write a small test that records what the code does now, even if you suspect it is wrong. Run it before you change anything. Then your fix shows up as a change you chose, not a surprise.

1. **Pick one input:** Use the report's input, or the smallest input that shows the bug.
2. **Record the output:** Save what the code returns today. That is your starting point.
3. **Fix, then compare:** Change the cause, run the test again, and explain every difference.

> **Tip:** Name the risk out loud: without a test, the same bug can come back.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A method you follow every time: read, reproduce, find the cause, fix, prove.
- [ ] Evidence for the cause: a debugger, a log or the stack trace, not a guess.
- [ ] A small fix in the right place, and a check of the other callers.
- [ ] A test that fails before the fix and passes after.
- [ ] Clear talk about what you did not check, and what you would check next.
- [ ] The reason behind each change, said in one sentence.

**Common traps**

- Trap: "Rewrite the whole module, it's faster." Better: A rewrite can add new bugs and hide the cause. Fix the cause in the smallest place, then check the callers.
- Trap: "It works now, so I'm done." Better: Show the test that failed before and passes now, and say what else you ran.
- Trap: "This line looked wrong, so I changed it." Better: Guesses waste the clock. Reproduce first, then use a debugger or a log to see the real values.

Palantir's interview post, which collects advice from its own team members, makes a similar point:

> "You don't need to remember the algorithm to a question." (Palantir blog, a team member's account)
>
> "One thing I found really helpful was the technique of deconstructing a problem and articulating how you would address it step-by-step." (Palantir blog, a team member's advice)

## Can you spot the bug?

**Spot the bug:** A teammate sent four small functions. Which one causes claims to share files?

A. def total(lines): return sum(line.amount for line in lines)
B. def add_file(claim, file, files=[]): files.append(file); return files
C. def is_late(due, paid): return paid > due
D. def claim_ids(claims): return [c.id for c in claims]

**Answer:** B. The empty list in the function's first line is made once, when the def statement runs. Every call that skips the argument adds to the same list. Use None as the default and make the list inside the function.

**Quick check:** A bug appears only when many users hit the app at once. What is the best first step?

A. Reproduce it by sending the same requests at the same time, as the report describes
B. Add a lock around the whole function, to be safe
C. Add more servers so each one takes fewer users
D. Rewrite the busy part in a faster language

**Answer:** A. Until you can make the bug happen under load, you cannot show where it comes from or prove a fix works. A lock, more servers or a rewrite may hide the problem without proving anything.

## Can you answer these from memory?

**Flash cards**

- **What is the first step in this round?** Reproduce the bug with the smallest input that makes it fail.
- **What does a stack trace show?** The chain of calls that led to the error. The last line names the error.
- **Why did the default list leak?** The default is made once, when the def line runs, and every call shares it.
- **What is a regression test?** A test that fails on the bug and passes after the fix, so it can't come back quietly.
- **What does pdb do?** Python's debugger. It pauses the code and lets you step line by line and inspect values.
- **What do you say at the end?** The cause, the fix, the callers you checked, and what you did not check.

## Likely follow-up questions

- Q402 Learning round: pick up a new codebase or API fast. https://designervenkat.online/interview/ai/learning-round-pick-up-a-new-codebase-or-api-fast
- Q151 "The answer was in the docs, but the bot missed it." How do you debug? https://designervenkat.online/interview/ai/debug-a-rag-bot-that-missed-the-answer
- Q190 How do you trace and debug an agent run? https://designervenkat.online/interview/ai/how-do-you-trace-and-debug-an-agent-run
- Q484 Refactor messy code into testable code. https://designervenkat.online/interview/ai/refactor-messy-code-into-testable-code
- Q485 Write tests and evals for an LLM function. https://designervenkat.online/interview/ai/write-tests-and-evals-for-an-llm-function
- Q275 Eval scores dropped but the model is unchanged. How do you debug? https://designervenkat.online/interview/ai/eval-scores-dropped-model-unchanged
