AP CSA 2.9 Debugging: The Maximum That Cannot Be Negative

Unit 2: Selection and Iteration · Lesson 2.9 · Debugging

The Maximum That Cannot Be Negative

Implementing Selection and Iteration Algorithms. 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

Initializing a running maximum to zero works on every test case you are likely to type by hand, and fails the moment every value is negative. This is the classic silent bug: correct-looking code, correct output all week, wrong answer on the one input that matters.

What is wrong with it

  1. Run the program on a list where every value is negative. It reports a maximum of 0, which is not even in the list. Initialize the running maximum from the data itself rather than from 0.
  2. The count of values equal to the maximum is always 1. The counter is being reset inside the loop instead of accumulated. Fix it so a list with three copies of the largest value reports 3.
  3. The minimum is computed correctly. Compare how it is initialized to how the maximum is initialized, and notice that the same fix pattern applies.

The program reads

An integer count of at least 1, then that many integers.

The program should print

Three lines: the largest value, the smallest value, and how many times the largest value appears.

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
4
-5 -2 -9 -7
Example 1 output
-2
-9
1
Example 2 input
5
3 9 4 9 1
Example 2 output
9
1
2

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

A running maximum initialized to 0 is really claiming that 0 is a member of the list. If every value is negative, no element ever beats 0, and the loop reports a number that was never in the data. Start from data[0] and scan from index 1.

Hint 2

times = 1 assigns; times++ accumulates. Assignment throws away everything counted so far, which is why the answer is always exactly 1 whenever the maximum appears at all.

Hint 3

The minimum loop is the shape you want: initialize from the first element, then start the scan at index 1. Make the maximum match it.

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]