Coding
AI Prompt for Finding Logic Bugs
Use this AI prompt for finding logic bugs when your code runs without errors but produces the wrong result — includes trace tables and test cases.
The prompt
Role: You are a meticulous debugging specialist who finds logic errors — bugs where code runs without crashing but produces wrong results. Context: My [LANGUAGE] code runs without errors, but the output is wrong. I am a [BEGINNER/INTERMEDIATE/ADVANCED] developer. Code: [CODE_SNIPPET] Expected behavior: [EXPECTED_BEHAVIOR] Actual behavior: [ACTUAL_BEHAVIOR] Sample input and wrong output: [SAMPLE_INPUT_AND_OUTPUT] Task: 1) Trace through the code step by step with my sample input, showing the value of key variables at each step. 2) Pinpoint the exact line(s) where the logic goes wrong and explain the flawed assumption. 3) Provide the corrected code with minimal changes. 4) Give me 3 additional test cases (including edge cases) that would have caught this bug, with expected outputs. Format: Start with the trace table, then the diagnosis, then the fixed code block, then the test cases in a simple list. Constraints: Do not just give the fixed code — I want to understand the mistake so I learn from it. Keep the fix minimal and preserve my variable names and code style.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[LANGUAGE] [BEGINNER/INTERMEDIATE/ADVANCED] [CODE_SNIPPET] [EXPECTED_BEHAVIOR] [ACTUAL_BEHAVIOR] [SAMPLE_INPUT_AND_OUTPUT]
Example input
LANGUAGE=JavaScript, expected: function returns the 3 cheapest products, actual: returns 3 random products, sample: array of 5 products with prices
How to use this prompt
Always include a concrete sample input with the wrong output — tracing is far more accurate than abstract reasoning. State the expected behavior in one plain sentence before pasting code.
Tips for the best results
- Ask for the trace table first if you only want to understand the bug, not the fix.
- Run the AI's suggested test cases yourself to confirm the fix actually works.
- Common culprits: off-by-one errors, wrong comparison operators, and mutated variables — mention if you suspect one.
Frequently asked questions
How is this different from a runtime error prompt?
Runtime errors crash with a message; logic bugs silently give wrong answers. This prompt forces step-by-step tracing, which is the right technique for silent bugs.
Can it help with bugs I cannot reproduce reliably?
Partially. Give it every input you remember and ask it to list inputs that could trigger the wrong branch — then try to reproduce with those.