When you ask AI to write or fix code, how do you write the prompt so the result is usable?
Suggested answer
The interviewer isn't looking for a magic phrase. They want to know whether you stay in control or just paste a request and accept whatever code comes back. A good answer names three things every prompt should make clear:
- What to do, with enough context: which files, which spec, the conditions it runs under (for example, tests run with
TZ=UTC). - What not to do: "don't write code yet, ask about anything unclear", "don't change the tests", "don't add libraries", "fix only issue number 1". Without constraints, AI tends to take the shortest path, like editing a test until it's green.
- How the result will be checked: "run the tests and paste the output", "the new tests must fail on the current code", "for every claim, give me a command I can run to verify it".
Tell a concrete example. For instance: before changing a command in an app, you asked AI to list the inputs most likely to go wrong, ran them yourself, and found that the message "/total month 10" was recorded as an expense of 10 đồng (Vietnam's currency). Then you wrote failing tests first, and only then asked AI to fix the code without touching the tests.
Bonus points: say that the checking step is the part you don't hand over to AI. An explanation that sounds reasonable isn't necessarily right, so you run the commands, measure, read the diff, and only then accept it.