AP CSA 4.4 Debugging: Find the Off-By-One

Unit 4: Data Collections · Lesson 4.4 · Debugging

Find the Off-By-One

Array Traversals. 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

This program is not a blank editor. It is somebody else's attempt, and it almost works: two real bugs are hiding in it, the kind that show up as a crash and the kind that shows up as a wrong answer nobody notices until a tie happens. Finding a bug in code you did not write is its own skill, distinct from writing code that has none yet.

What is wrong with it

  1. Run this program on the sample input. It crashes with an ArrayIndexOutOfBoundsException while printing the array backward. Find the loop bound that reads one index past the end of the array and fix it.
  2. Once it runs without crashing, it still disagrees with the expected output whenever the largest value appears more than once: it reports the LAST index that value appears at instead of the FIRST. Fix the comparison so a tie keeps the earlier index.
  3. The forward print and the sum of the even-index elements are already correct. Do not rewrite them; the fix is smaller than it looks.

The program reads

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

The program should print

Every element forwards, then backwards, then the FIRST index of the largest value, then the sum of the elements at even indexes.

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

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

The crash happens on the very first line of the backward loop, before anything prints. Valid indexes run from 0 to length - 1; length itself is one past the end.

Hint 2

A tie for the largest value should keep the FIRST index it appeared at. A strict > only replaces best when a later value is bigger, so an equal later value leaves the earlier index alone. >= replaces it anyway.

Hint 3

The forward print and the even-index sum never needed a fix. If your diff touches those lines, you changed something that was not broken.

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]