Where is the logic wrong in my code?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Logic errors are bugs where the code runs without crashing but produces incorrect results. Unlike syntax errors (which the compiler catches) or runtime errors (which throw exceptions), logic errors are silent — the program behaves differently from what you intended. Finding them requires understanding common patterns that lead to wrong output: off-by-one errors, incorrect boolean conditions, wrong operator precedence, unintended integer division, and mutation of shared state.
Off-by-One Errors
The most common logic error. Happens with loop boundaries, array indices, and string slicing:
Incorrect Boolean Logic
Confusing AND/OR, negation, and De Morgan's laws:
Operator Precedence
Integer Division and Floating Point
Mutation of Shared State
Wrong Variable Scope
Debugging Strategies
Common Pitfalls
- Confusing
=and==: In languages like C, Java, and JavaScript,if (x = 5)is an assignment (always truthy), not a comparison. Python prevents this with a syntax error, but==vsisis a similar trap for object identity checks. - Short-circuit evaluation surprises:
if a and bdoes not evaluatebifais falsy. Ifbhas side effects (like function calls), they are skipped. Similarly,if a or bskipsbifais truthy. - Return inside a loop: Placing
returninside a loop exits on the first iteration. If you intend to process all items, accumulate results and return after the loop ends. - Forgetting break in switch/case (C, Java, JS): Without
break, execution falls through to the next case. Python'smatch/casedoes not have this problem, but C-family languages do. - Using
==for floating point comparison: Floating point arithmetic introduces rounding errors.0.1 + 0.2 != 0.3in most languages. Use an epsilon tolerance or dedicated comparison functions.
Summary
- Logic errors produce wrong results without crashing — they are the hardest bugs to find
- Off-by-one errors are the most common: double-check loop boundaries and index ranges
- Verify boolean conditions with truth tables, especially when using
and,or, andnot - Watch for integer division truncation, floating point imprecision, and mutable default arguments
- Use print debugging, assertions, and edge-case tests to isolate where the logic diverges from intent
Related reading
- Which built-in .NET exceptions can I throw from my application?
- Which exception should I raise on bad/illegal argument combinations in Python?
- Which exception should I raise on bad/illegal argument combinations in Python?
- Which Java thread is hogging the CPU?
- Which part of throwing an Exception is expensive?
- Which status code should I use for failed validations or invalid duplicates?
- Which version of PostgreSQL am I running?
- Which version of Python do I have installed?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.