AP CSA 2.12 Debugging: Counting the Wrong Operations

Unit 2: Selection and Iteration · Lesson 2.12 · Debugging

Counting the Wrong Operations

Informal Run Time Analysis. This is not a blank editor: it is someone else's attempt. Find what is wrong, fix it, and submit to be graded against hidden test cases.

Why debugging is its own skill

Run time analysis is counting, and counting is only meaningful if the counter sits where the work happens. Put the increment one block too high and a quadratic algorithm reports linear growth, which is exactly the wrong lesson.

What is wrong with it

  1. The comparison counter for the nested pass is incremented in the OUTER loop body instead of the inner one, so it reports n instead of n squared. Move it to where the comparison actually happens.
  2. The single pass is supposed to report n - 1 comparisons, since comparing n items pairwise takes one fewer comparison than there are items. Its loop starts at 0 and reports n. Fix the start value.
  3. Do not change the loop bounds of the nested pass. The point of the exercise is that its count should grow like n squared, and it already visits the right pairs.

The program reads

A single integer n of at least 2.

The program should print

Two lines: the number of comparisons a single pass makes, then the number a nested pass makes.

Worked examples

These show what the FIXED program should print. There are more cases you cannot see, and they use different values, so patching around just these numbers will fail.

Example 1 input
5
Example 1 output
4
25
Example 2 input
2
Example 2 output
1
4

Your answer

Main.java

Input for the Run button

Run sends whatever is in the code box below, bugs and all, so you can see the crash or the wrong answer for yourself before you fix anything.


  

  

Stuck?

Hint 1

Run the starter with n = 5 and then n = 10. The nested count doubles when n doubles, which is the signature of a linear algorithm. A genuinely nested loop should quadruple. That gap between the shape of the code and the shape of the number is the bug.

Hint 2

Every statement inside the inner loop runs n times per outer pass, for n * n total. Every statement in the outer body runs only n times. Where you put the increment decides which number you are measuring.

Hint 3

For the single pass, think about comparing neighbors in a list of n items: 5 items have only 4 neighboring pairs. Starting the loop at 1 instead of 0 is the whole fix.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]