AP CSP Day 35: Lists as Data Abstraction | Cycle 2

Key Concepts

List index errors are among the most common bugs in list-based algorithms, occurring when code accesses an index that does not exist in the list. In AP CSP pseudocode, lists are 1-indexed, so accessing index 0 or an index greater than the list length is an error. Cycle 2 list questions present algorithms that appear correct but contain an off-by-one index error or attempt to access a list element that may not exist under certain input conditions. Identifying the specific input that triggers an index error is a key error-analysis skill.

📚 Study the Concept First (Optional) Click to expand ▼

List Index Errors

Off-By-One Index Errors

An off-by-one error accesses the element before or after the intended one. In AP CSP (1-indexed), accessing index length(list) gives the last element correctly, but accessing length(list)+1 is out of bounds.

Empty List Edge Case

An algorithm that assumes the list has at least one element will fail when given an empty list. Well-designed list procedures always handle the empty list case explicitly.

Common Trap: Testing algorithms only with populated lists. Empty lists and single-element lists are the most common edge cases that expose index errors.
Exam Tip: When tracing a list algorithm on the AP exam, check: what happens when the list is empty? What happens for the first element? What happens for the last element? These three cases catch most index bugs.
Big Idea 2: Data
Cycle 2 • Day 35 Practice • Hard Difficulty
Focus: Lists as Data Abstraction

Practice Question

What is displayed after the following code runs?

data ← [5, 10, 15, 20, 25]
INSERT(data, 3, 12)
REMOVE(data, 5)
DISPLAY(data[4])
Why This Answer?

After INSERT(data, 3, 12): 12 is inserted at index 3, shifting elements right. List becomes [5, 10, 12, 15, 20, 25]. After REMOVE(data, 5): element at index 5 (which is 20) is removed. List becomes [5, 10, 12, 15, 25]. Now data[4] = 15.

Why Not the Others?

B) 20 was removed from the list. C) 25 is at index 5 after the removal, not index 4. D) 12 is at index 3, not index 4.

Common Mistake
Watch Out!

Students forget that INSERT shifts all later elements right by one index, and then REMOVE shifts elements back. Tracking the list state after each operation is essential.

AP Exam Tip

After every INSERT or REMOVE, rewrite the entire list with updated indices. Do not try to track changes mentally — write it out.

Keep Practicing!

Consistent daily practice is the key to AP CSP success.

AP CSP Resources Get 1-on-1 Help
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.