Quarry School

Check a procedure against a counterexample

Subject: Computing
Level 1 · FoundationsAbout 6 min read3 new terms
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    A procedure can look right and be wrong.

    Rule

    A test case is an allowed input together with the output expected for it. In this unit the expected output is worked out by hand before the procedure is carried out. A procedure passes a test when it announces that output. An allowed input for which it announces anything else is a counterexample, and one counterexample shows that the procedure does not solve the problem. Passing tests cover only the inputs tried.

    Step

    1. Take the table of inputs and correct outputs made with the problem statement, as in lesson 1.

    2. Add inputs at the extremes of what is allowed, such as the smallest allowed row. Add unusual rows too: a row with a repeated value, and a row whose numbers are all below zero.

    3. Trace the procedure on each input, and write what it announces beside the expected output.

    4. Mark each row pass or fail. A failed row is a counterexample.

    5. For a counterexample, identify the error and correct it. One way to find it is to look for the first step where the trace differs from a correct run. Correct that step and any later step that depends on it.

    6. Carry out every test again after the correction, including the tests that passed before.

    Trap

    After several passing tests it is tempting to call the procedure correct. The three rows that pass all hold a number above 0, which hides the mistake. The changed procedure fails only on rows whose numbers are all below 0. Choose tests that differ from each other in kind, including inputs at the extremes of what is allowed.

    A second slip is patching the counterexample alone, for instance with a new step that announces -3 for that one row. The row -9 -4 would then fail in the same way. Correct the steps that cause the error by restoring the first two steps of the largest-card procedure of lesson 2. Step 1 takes the first card and writes its number on the slip, and step 2 goes on with the cards that are left.

    1 In plain words

    A procedure can look right and be wrong. One way to find out is to carry it out on inputs chosen to make it fail.

    Testing uses chosen inputs to check that a procedure produces the expected outputs. An allowed input for which the procedure gives a wrong output is a counterexample. The mistake it exposes is a logic error, which is a mistake in an algorithm that makes it behave incorrectly.

    One counterexample is enough to show that a procedure does not solve its problem, because an algorithm gives the correct result every time.

    Here is a changed version of the largest-card procedure with a logic error in its first step. Step 1: write 0 on the slip as the noted number. Step 2: if no card is left in the row, go to step 5, and otherwise take the next card, starting with the first. Step 3: if the card's number is larger than the noted number, cross out the noted number and write the card's number in its place. Step 4: go back to step 2. Step 5: announce the noted number and stop. Testing with chosen inputs is how this lesson finds the error.

    Remember one sentence: one wrong output outweighs any number of right ones. For testing, find essential knowledge CRD-2.J.1 in the AP Computer Science Principles Course and Exam Description.

    2 Picture it

    RowExpectedAnnounced4 9 2997775 5 355-3 -8-30

    A table of four tests of the changed procedure, with the expected output and the announced output of each. For the row 4 9 2 both are 9. For the row 7 both are 7. For the row 5 5 3 both are 5. For the row -3 -8 the expected output is -3 and the procedure announces 0. That last row is marked as the counterexample.

    Three rows pass and the fourth is a counterexample

    3 Why

    The changed procedure starts with a noted number of 0, whether or not any card shows 0. For a row that holds a number above 0, the first such card replaces the 0 on the slip, and the mistake stays hidden. For the row -3 -8, no card is larger than 0, so the slip is never changed and the procedure announces 0, a number that is not in the row.

    Three tests passed before the fourth showed the mistake. Passing tests show that a procedure works for the inputs tried. They do not show that it works for every allowed input. That is one reason to include inputs at the extremes of what is allowed among the tests.

    4 The rule

    A test case is an allowed input together with the output expected for it.

    In this unit the expected output is worked out by hand before the procedure is carried out. A procedure passes a test when it announces that output. An allowed input for which it announces anything else is a counterexample, and one counterexample shows that the procedure does not solve the problem. Passing tests cover only the inputs tried.

    5 How

    1. Take the table of inputs and correct outputs made with the problem statement, as in lesson 1.
    2. Add inputs at the extremes of what is allowed, such as the smallest allowed row. Add unusual rows too: a row with a repeated value, and a row whose numbers are all below zero.
    3. Trace the procedure on each input, and write what it announces beside the expected output.
    4. Mark each row pass or fail. A failed row is a counterexample.
    5. For a counterexample, identify the error and correct it. One way to find it is to look for the first step where the trace differs from a correct run. Correct that step and any later step that depends on it.
    6. Carry out every test again after the correction, including the tests that passed before.

    6 Trace

    Trace the changed procedure on the row -3 -8
    StepCard takenNoted numberExplanation
    1—0The slip starts at 0, a number that is on no card of this row.
    2-30Take the first card.
    3—0The card shows -3, and -3 is not larger than 0. The slip stays as it is.
    4—0Go back to step 2.
    2-80Take the next card.
    3—0The card shows -8, and -8 is not larger than 0. The slip stays as it is.
    4—0Go back to step 2.
    2none0No card is left. Go to step 5.
    5—0Announce 0. The expected output is -3, so this row is a counterexample.

    7 The trap

    After several passing tests it is tempting to call the procedure correct. The three rows that pass all hold a number above 0, which hides the mistake. The changed procedure fails only on rows whose numbers are all below 0. Choose tests that differ from each other in kind, including inputs at the extremes of what is allowed.

    A second slip is patching the counterexample alone, for instance with a new step that announces -3 for that one row. The row -9 -4 would then fail in the same way. Correct the steps that cause the error by restoring the first two steps of the largest-card procedure of lesson 2. Step 1 takes the first card and writes its number on the slip, and step 2 goes on with the cards that are left.

    Quick checks

    Quick check 1 of 2

    Trace the changed procedure, the one that starts with 0 on the slip, on the row -5 -1 -7. What does it announce, what is the correct output, and is the row a counterexample?

    Show the steps and the answer
    Answer

    It announces 0. The correct output is -1. The row is a counterexample.

    Why: No card is larger than 0, so the slip never changes. The largest number in the row is -1, and the procedure announces something else.

    Source: College Board, AP Computer Science Principles Course and Exam Description

    Quick check 2 of 2

    The changed procedure passes the tests 4 9 2, 7 and 5 5 3. A classmate says that three passes show it is correct. What is wrong with that claim, and what kind of row tests it?

    Show the steps and the answer
    Answer

    Passing tests cover only the rows tried. A row in which every number is below 0, such as -3 -8, makes the procedure announce 0.

    Why: A procedure solves the problem only if it is right for every allowed row. The three rows all hold a number above 0, so none of them reaches the case that the first step gets wrong.

    Source: College Board, AP Computer Science Principles Course and Exam Description

    Sources

    1. AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 1 Dec 2026.
    2. Computer Science Field Guide: Algorithms, 2.1 What's the big picture?. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 1 Dec 2026.