Teams often inherit legacy code that is hard to understand, tightly coupled, and missing automated tests. The challenge is improving structure without accidentally changing behavior in production.
Explain how you would safely refactor a legacy piece of code that has no tests.
Your answer should cover:
The interviewer expects a practical engineering approach, not just a definition of refactoring. Discuss the order of operations, how to handle unknown behavior, how to validate changes, and how you would decide whether to refactor in place, wrap the code, or extract smaller units first.