Problem Solving — CS & DS

Lecture 12 — Debugging Lab: Real Defects

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The median that lies

cs-045

A statistics function returns the wrong median for even-length input (right for odd). Diagnose; decide the empty case.

Expected: 3 for odd; 2.5 for even; empty: define or reject

Cost: O(n log n); selection algorithms (lecture 18's kth-element) do better - a teaser.

Worked example

The phantom zero

cs-046

Readings [0, 5, 0, 7] with 'stop reading at 0'. The reference prints [] (eaten) vs [0, 5, 0, 7] (kept).

Expected: [] (broken) vs [0, 5, 0, 7] (fixed)

Cost: O(n) either way; the content is contract design.

Worked example

The swap that wasn't

cs-047

Tuple-assignment swaps inside a loop that also reassigns indices: the reference prints [3, 2, 3] - one element duplicated, one lost. Diagnose with a memory diagram.

Expected: [3, 2, 3] (buggy) vs the intended swap after the fix

Cost: Constant; binding-order reasoning returns in cs-048 and cs-055.

Worked example

The regression zoo: three defects, one protocol

cs-048

Snippets: (a) counter defined outside a function, (b) shared default list, (c) range(len(xs)) deletion loop. Reference: crash 'list index out of range'; fixed [1, 5] and [1, 2] [1, 2].

def median(xs):
    s = sorted(xs)
    n = len(s)
    mid = n // 2
    if n % 2 == 1:
        return s[mid]
    return (s[mid - 1] + s[mid]) / 2

print(median([3, 1, 2]), median([4, 1, 3, 2]))

Expected: buggy crashed: list index out of range | fixed: [1, 5] and [1, 2] [1, 2]

Cost: All O(n); three defect classes in one hour, each fixed under the full protocol.

Case attempt · lecture 12
Case cs-045 · Beginner

The Median That Lies

A median function returns wrong values for even-length lists. Localise, fix, and add regression tests so it never regresses.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-045 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-045 — not embedded in this deck.

Case attempt · lecture 12
Case cs-046 · Foundational

The Phantom Zero

A sentinel value of 0 corrupts statistics for legitimate zeros in the data. Diagnose the sentinel choice and redesign the loop's exit.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-046 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-046 — not embedded in this deck.

Case attempt · lecture 12
Case cs-129 · Foundational

The Missing Marks

The exams office exports this file, `marks.csv`, exactly as written:

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-129 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-129 — not embedded in this deck.

Case attempt · lecture 12
Case cs-047 · Intermediate

The Swap That Wasn't

An attempted in-place swap of list elements fails silently for one ordering. Trace it, find why, and enumerate every ordering in a test.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-047 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-047 — not embedded in this deck.

Case attempt · lecture 12
Case cs-130 · Intermediate

The Duplicate Rows

Two registration exports must describe the same class of students.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-130 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-130 — not embedded in this deck.

Case attempt · lecture 12
Case cs-048 · Advanced

The Regression Zoo

Three small programs, each with a distinct classic defect class (scope, mutation, boundary). Localise all three under a time budget, then rank difficulty.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-048 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-048 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Why must the reproduction step precede any hypothesis?
  2. What does 'fix without breaking green tests' require?
  3. Name the isolation technique used on cs-048.
Expected answers (instructor): an unreproduced defect cannot be falsified; you would be guessing a regression suite run before and after every change history bisect: halve the commit range each experiment
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF