Practice Debugging in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick 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.

Showing a random 20 of 50 problems.

Example 1

easy
A function works for input 55 but crashes for input 00. What category of test catches 00?

Example 2

medium
`total = 0; FOR i = 1 TO 4: total = total + i; OUTPUT total`. What does it print?

Example 3

easy
A code uses `if name = 'Sam':` and errors. What's wrong?

Example 4

medium
The code `SET i = 0; WHILE i <= 5: OUTPUT i` prints how many numbers?

Example 5

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

Example 6

medium
A loop `i = 0; while i < 5: print(i)` never stops. What's missing?

Example 7

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.

Example 8

easy
What is the second step (after reproducing) in systematic debugging?

Example 9

medium
A bug only happens on the build server, not your laptop. What general kind of factor often causes this?

Example 10

medium
Output is right for input 5 but wrong for input 0. Which test category caught this?

Example 11

medium
`for item in items: items.remove(item)`. Why is this dangerous?

Example 12

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 13

hard
A function reads a global config and behaves differently between test and prod. What is the hidden coupling?

Example 14

easy
A function returns the wrong answer. To isolate the cause, what tool prints intermediate values?

Example 15

easy
A bug keeps coming back in different forms. What did the previous fix likely address?

Example 16

easy
What does 'reproduce the bug' mean?

Example 17

medium
`def avg(nums): return sum(nums)/len(nums)`. Calling `avg([])` errors. What's the edge case and a fix?

Example 18

easy
What is the term for an error in a program?

Example 19

challenge
Code reads a global `config` and behaves differently in tests vs production. What hidden factor is causing the inconsistency?

Example 20

medium
`x = [1,2,3]; for i in x: x.append(i)`. Why is this dangerous?