AP CSP Day 35: Lists as Data Abstraction | Cycle 2
Share
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.
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])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.
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.
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.
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