Finding, Explaining and Preventing the Bug
45 min · Intermediate
In a find-the-bug round the interviewer shows a short method that "sometimes gives the wrong answer" and watches how you find the bug, fix it and prove the fix. A calm method beats staring: read the contract, try small and edge inputs, and explain the cause, the fix and the test that proves it.
Topics in this lesson
- A method for reading unfamiliar code
- Classic bugs: the short list to check every time
- Explaining the fix aloud: cause, fix, test
- Prevention: unit tests, static analysis, code review
- Worked examples: five short buggy methods
Interview questions this lesson answers
- How do you approach a find-the-bug question?
- What is an off-by-one error?
- Why do edge cases find more bugs than normal cases?
- How do you explain a bug to the interviewer?
- What is the difference between fixing the root cause and fixing the symptom?
- What is switch fall-through and how do you avoid it?
- Why is max = 0 a bug when finding the maximum?
- How do you avoid NullPointerException bugs?
- How can integer overflow cause a bug that looks correct?
- Why does a swap method with int parameters not work?
Sign in free to continue this lesson
Create a free account to read every lesson, practise with the exercises and keep track of your progress.