Collaborative Learning Activity: "The Debugging Dojo"
Activity Type: Small Group Challenge (Pairs or Trios)
Time Allotment: 45-60 minutes
Learning Objectives:
● Practice the systematic process of debugging.
● Develop skills in forming and testing hypotheses.
● Utilize peer discussion and "rubber ducking" to solve problems.
● Experience different debugging tools and strategies.
Materials Needed:
● Pre-written buggy code snippets (provided below).
● Computers with a relevant programming environment/IDE installed.
● (Optional) Printed copies of the code snippets and worksheets.
The Challenges
Groups can work on these chellenges in sequence, or you can assign different
challenges to different groups.
Challenge 1: The Logical Labyrinth
Language: Python
Task: This function is supposed to find the largest number in a list. It sometimes works
but often returns the wrong value. Find and fix the bug.
python
def find_largest(numbers):
largest = 0
for num in numbers:
if num > largest:
largest = num
return largest
# Test Cases (Provide these to students)
print(find_largest([1, 5, 3, 9, 2])) # Expected: 9, Actual: 9 (seems ok!)
print(find_largest([-5, -2, -10])) # Expected: -2, Actual: 0 (WRONG!)
print(find_largest([3, 3, 3])) # Expected: 3, Actual: 3 (ok)
Guiding Questions for Debrief:
● What was your initial hypothesis when the first test case passed?
● How did the second test case change your hypothesis?
● What is the flaw in the initial assumption of setting largest = 0?
Challenge 2: The Off-by-One Odyssey
Language: Java
Task: This function should print a countdown from a given number n down to 1. It's off
by one.
java
public class Counter {
public static void countdown(int n) {
for (int i = n; i >= 0; i--) {
[Link](i);
[Link]("Blastoff!");
public static void main(String[] args) {
countdown(5);
// Expected: 5, 4, 3, 2, 1, Blastoff!
// Actual: 5, 4, 3, 2, 1, 0, Blastoff! (WRONG!)
}
Guiding Questions for Debrief:
● How did you localize the issue? Did you use the debugger to step through the
loop?
● What is the precise loop condition that causes the extra number to be printed?
● Why are "off-by-one" errors so common?
Challenge 3: The Resource Riddle
Language: Python (simulating a common bug)
Task: This code is meant to read a file and print each line. It works but is inefficient and
could lead to problems with larger files. What's the issue, and how do you fix it?
python
def print_file_lines(filename):
file = open(filename, 'r')
lines = [Link]()
for line in lines:
print([Link]())
# The function is called
print_file_lines('[Link]')
Guiding Questions for Debrief:
● What happens if an exception is thrown before [Link]() is called?
● What is the best practice for handling resources like files in your language?
(Answer for Python: use a with block).
● This isn't a "crashing" bug. Why is it still considered a bug?
Part 3: Group Debrief and Reflection (10-15 minutes)
Bring the entire class back together. For each challenge, ask one or two groups to
present their findings.