This code calculates an average but gives the wrong answer: SET total = 0. FOR each num in [10, 20, 30]: total = total + num. SET average = total / 2. OUTPUT average. The input has 3 numbers. Find the bug.
Example 3
medium
The code `SET i = 0; WHILE i <= 5: OUTPUT i` prints how many numbers?
A bug is somewhere in 32 commits. Using bisection (halve each time), what is the worst-case number of test runs?
Example 6
medium
This computes a maximum: `max = nums[0]; FOR i = 1 TO LENGTH(nums)-1: IF nums[i] > max THEN max = nums[i]`. For nums = [4, 9, 2, 7], what does it return?
Example 7
hard
This binary search bug: `low = 0; high = LENGTH(arr)-1; WHILE low <= high: mid = (low + high) / 2; ...`. In integer division on negatives, what may go wrong if low+high is negative due to signed underflow?
A buggy function returns `nums[i+1]` where nums has length n and i runs up to n−1. What error happens at the last iteration and how do you fix the index bound?
Practice Problems
Try each one, then open the solution to compare your method.
Example 1
medium
This code should output the largest number in [3, 7, 2, 9, 4] but outputs 3: SET largest = nums[0]. FOR i = 0 TO LENGTH(nums)-1: IF nums[i] > largest THEN largest = nums[i]. OUTPUT largest. Trace and verify if it is correct, or find the bug.
Example 2
medium
A program to sum the numbers 1 through 5 uses `SET total = 1; FOR i = 1 TO 5: total = total + i`. It outputs 16 instead of 15. Identify the bug and fix it.
Example 3
easy
What is the FIRST step of systematic debugging?
Example 4
easy
A program crashes only on empty input. What kind of value triggers many bugs and should be tested?
Example 5
easy
`if x = 5:` causes an error in many languages. What's the bug?
Example 6
easy
After fixing a bug, what must you always do before declaring it done?
Example 7
easy
A function returns the wrong answer. To isolate the cause, what tool prints intermediate values?
Example 8
easy
A bug keeps coming back in different forms. What did the previous fix likely address?
Example 9
easy
Changing many things at once then testing makes debugging harder. What's the better approach?
Example 10
easy
A stack trace points to line 42. What does that tell you?
Example 11
medium
`def avg(nums): return sum(nums) / len(nums)`. It crashes on `avg([])`. What is the bug and edge case?
Example 12
medium
A loop `i = 0; while i < 5: print(i)` never stops. What's missing?
Example 13
medium
Output is right for input 5 but wrong for input 0. Which test category caught this?
Example 14
medium
`def f(x): return x * 2` is expected to give 6 for input 3 but gives `33`. What does the actual behavior reveal about `x`?
Example 15
medium
Bisection debugging: a bug is somewhere in 16 commits. Roughly how many test runs (worst case) to isolate it by halving?
Example 16
medium
Two functions both look correct but together produce a wrong result. What kind of bug is this likely to be?
Example 17
medium
A bug only appears sometimes. What property makes it hard to debug?
Example 18
challenge
A function works for n=1,2,3 but fails for n=4 with a wrong sum. You suspect an off-by-one. In `total = 0; for i in 1..n: total += i` (range excludes n), what is computed for n=4 vs the intended 1+2+3+4?
Example 19
challenge
Code reads a global `config` and behaves differently in tests vs production. What hidden factor is causing the inconsistency?
Example 20
challenge
You fix bug A and immediately bug B appears in a previously-working feature. What is this called and what prevents it?
Example 21
medium
A test passes locally but fails on the build server. Changing one variable at a time, you find it's the OS file path separator. What debugging principle found it?
Example 22
medium
`x = [1,2,3]; for i in x: x.append(i)`. Why is this dangerous?
Example 23
easy
A code uses `if name = 'Sam':` and errors. What's wrong?
Example 24
easy
After a fix, what must you do before declaring the bug closed?
Example 25
easy
What does adding a `print(x)` statement help with?
Example 26
easy
A function works for input 5 but crashes for input 0. What category of test catches 0?
Example 27
medium
`total = 0; FOR i = 1 TO 4: total = total + i; OUTPUT total`. What does it print?
Example 28
medium
This code should sum 1..n: `total = 0; FOR i = 1 TO n: total = total + i`. For n=10 it should give 55. It returns 45. What's wrong?
Example 29
medium
`def avg(nums): return sum(nums)/len(nums)`. Calling `avg([])` errors. What's the edge case and a fix?
Example 30
medium
A bug only happens on the build server, not your laptop. What general kind of factor often causes this?
Example 31
medium
`x = '3'; y = 2; print(x + y)` errors. What's the bug, and how do you fix it for numeric addition?
Example 32
medium
`for item in items: items.remove(item)`. Why is this dangerous?
Example 33
medium
A test passes locally but fails in CI. Which debugging principle helps isolate the cause?
Example 34
hard
A function returns the WRONG answer for n=4, expected 1+2+3+4=10 but got 6. The body is `total=0; for i=1..n step 1: total+=i` where the range is exclusive of n. What did it sum, and what is the fix?
Example 35
hard
A regression appears: fixing bug A broke working feature B. What kind of automated test would have caught it earliest?
Example 36
hard
A function reads a global config and behaves differently between test and prod. What is the hidden coupling?
Example 37
challenge
A bug appears 1 in 100 runs and never with the debugger attached. What general category is this, and what tool helps?
What to focus on
Core idea:Debugging is systematic: reproduce the bug, isolate the cause, apply a fix, then verify it works.
Common stuck point:The bug might not be where the error appears—trace backward.
Ask yourself:Do you already have a program that does the wrong thing, and need to find and fix the cause?
Concept recap
The systematic process of finding, diagnosing, and correcting errors (bugs) in a program. Debugging involves reproducing the problem, isolating its cause through testing and inspection, applying a targeted fix, and verifying the fix resolves the issue without introducing new problems.
A good doctor does not hand out random medicine. They ask what is wrong, guess a cause, run one test to check the guess, then treat the cause instead of only the cough. Debugging follows the same order. One difference helps you: a program can be run again in exactly the same way, so you can repeat a test as often as you like.