Debugging Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Debugging.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

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.

Detective work—observe the wrong output, form a hypothesis, test it, then fix what's wrong.

Read the full concept explanation →

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

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.

Sense of Study hint: When debugging, follow these steps: first reproduce the bug reliably with a specific input. Then narrow down where the problem occurs by adding print statements or using a debugger to inspect variable values. Once you find the faulty line, fix it and test with the original failing input plus other cases.

Common Mistakes to Watch For

Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.

Worked Examples

Example 1

medium
This code should print numbers 1 to 5 but has a bug: SET i = 1. WHILE i < 5: OUTPUT i. i = i + 1. Find and fix the bug.

Answer

Change `i < 5` to `i <= 5`. Off-by-one error.

First step

1
Step 1: Trace: i=1 (output 1), i=2 (output 2), i=3 (output 3), i=4 (output 4), i=5 (5 < 5 is false, loop ends).

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan — every worked solution, all subjects

Example 2

medium
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?

Example 4

medium
`if score >= 90: grade = 'A' elif score >= 80: grade = 'B' elif score >= 70: grade = 'A'`. A student scores 75 and gets 'A'. Find the bug.

Example 5

medium
A bug is somewhere in 3232 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?

Example 8

hard
Trace this: `a=[1,2,3]; b=a; b.append(4); print(len(a))`. Output?

Example 9

challenge
A buggy function returns `nums[i+1]` where nums has length nn and i runs up to n1n-1. What error happens at the last iteration and how do you fix the index bound?

Practice Problems

Try these problems on your own first, 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 55 but crashes for input 00. What category of test catches 00?

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=101+2+3+4=10 but got 66. 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?

Background Knowledge

These ideas may be useful before you work through the harder examples.

algorithm