Lesson 5: Bug Hunters — Finding and Fixing Broken Algorithms

Learning objective

Students will systematically debug broken grid algorithms (including sequences, REPEAT loops, and IF conditionals) by tracing step by step to find the exact line where actual state diverges from expected state, classifying the bug, and testing a fix.

Materials

Vocabulary

Lesson flow

1. Warm-up: Why Do We Call It a "Bug"? & The Three Classic Bug Types (8 min)

  1. Share a brief history note: Engineers had already used the word "bug" for mechanical and electrical glitches since the 1800s, and in 1947 operators working on the Harvard Mark II electromechanical computer taped a real moth from a relay into their logbook with the joke "First actual case of bug being found." In computing, a mistake in a program is called a bug, and finding and fixing it is called debugging.
  2. Introduce the Three Classic Grid Bugs on the board:
  3. Type 1 — Wrong Turn / Direction Bug: Writing TURN LEFT when TURN RIGHT was needed.
  4. Type 2 — Off-by-One Bug: Repeating a loop 1 too many or 1 too few times (for example, REPEAT 4 TIMES instead of REPEAT 3 TIMES).
  5. Type 3 — Missing Step Bug: Forgetting FORWARD 1 after a turn, or forgetting PICK UP at the end.

2. Direct instruction: The Golden Rule of Debugging (10 min)

Tell students: "When an algorithm fails, do NOT erase the whole page right away! Often many of the steps already work as intended, and we only need to repair the specific step where the robot goes off track. Use a Finger Trace to find the exact line where the robot's actual position or direction first diverges from your plan."

Model Case 0 on the board: - Setup: Start at A1 facing North. Goal star ★ is at B3. Rock X is at A3. - Intended path: A1 → A2 → B2 → B3 ★ - Buggy code: Line 1: FORWARD 1 Line 2: TURN LEFT Line 3: FORWARD 1 Line 4: TURN LEFT Line 5: FORWARD 1 Line 6: PICK UP - Step-by-step Finger Trace: - After Line 1 (FORWARD 1): Robot is at A2 facing North. (Matches plan!) - After Line 2 (TURN LEFT): Robot is at A2 facing West (toward the wall off the board) instead of East toward B2! (Divergence found on Line 2!) - Fix: Change Line 2 from TURN LEFT to TURN RIGHT. Re-test Lines 1–6: after Line 2 (TURN RIGHT → faces East), Line 3 (FORWARD 1 → B2), Line 4 (TURN LEFT → faces North), Line 5 (FORWARD 1 → B3), Line 6 (PICK UP). Success!

3. Guided practice: Three Bug Hunter Case Files (17 min)

Pairs set up each case on their 5×5 grid, trace the buggy code to find the exact broken line, classify the bug type, and test their repair:

Case File 1: The Overshot Treasure (Off-by-One Bug)

Case File 2: The Spin-In-Place Trap (Missing Step Bug)

Case File 3: The Backwards Sensor (Conditional Bug)

4. Wrap-up & Unit Exit Check (10 min)

A robot starts at A1 facing North. The Star ★ is at C2. There are no rocks. A student wrote this 5-line program:

Line 1: FORWARD 1
Line 2: TURN RIGHT
Line 3: REPEAT 3 TIMES:
Line 4:     FORWARD 1
Line 5: PICK UP

Answer these three questions:

  1. Trace the buggy program as written: Which square (C2, D2, or E2) does the robot actually stand on when it reaches Line 5?
  2. Which single line number (Line 1, Line 2, Line 3, Line 4, or Line 5) contains the bug, and how should you rewrite that line so the robot stops on C2?
  3. Is this bug a Wrong Turn Bug, an Off-by-One Bug, or a Missing Step Bug?

Worked answers for exit check

  1. After Line 1 (FORWARD 1), the robot is on A2 facing North. After Line 2 (TURN RIGHT), it is on A2 facing East. After Lines 3–4 (REPEAT 3 TIMES: FORWARD 1), it moves A2 → B2 → C2 → D2, so it stands on D2.
  2. Line 3 has the bug. Rewrite Line 3 as REPEAT 2 TIMES: (so it moves A2 → B2 → C2 and stops on C2).
  3. It is an Off-by-One Bug (the loop repeated 3 times instead of 2 times).