Question 41
A neighborhood book club meets monthly to discuss and rate books. Members rate each book on a scale of 1 to 10, and the club maintains two parallel ArrayLists: one for book titles and one for their average ratings. The BookClub class provides methods to help members select their next read.
PART A
Write the method public ArrayList<String> getTopRated(ArrayList<String> titles, ArrayList<Integer> ratings, int threshold) that returns a new ArrayList containing only the titles whose corresponding rating is greater than or equal to threshold, preserving the original order.
For example, if titles is ["Dune", "1984", "Cosmos", "Beloved"] and ratings is [9, 7, 8, 6] with threshold 8, the method returns ["Dune", "Cosmos"].
public ArrayList<String> getTopRated(ArrayList<String> titles, ArrayList<Integer>
ratings, int threshold) {PART B
Write the method public String recommend(ArrayList<String> titles, ArrayList<Integer> ratings) that removes and returns the title with the highest rating. If there is a tie, remove and return the one appearing earliest in the list. Both the titles and ratings lists should have the corresponding element removed.
For example, if titles is ["Dune", "1984", "Cosmos"] and ratings is [9, 7, 9], the method returns "Dune" (first with highest rating 9) and both lists are reduced to ["1984", "Cosmos"] and [7, 9].
public String recommend(ArrayList<String> titles, ArrayList<Integer> ratings) {Main Solution
PART A — getTopRated
Algorithm: Create a new ArrayList<String>. Iterate through every index of titles. At each index, compare the corresponding element of ratings to threshold. If the rating is greater than or equal to threshold, add the title at that index to the result list. Return the result list.
public ArrayList<String> getTopRated(ArrayList<String> titles,
ArrayList<Integer> ratings, int threshold) {
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < titles.size(); i++) {
if (ratings.get(i) >= threshold) {
result.add(titles.get(i));
}
}
return result;
}Trace with example: titles = ["Dune", "1984", "Cosmos", "Beloved"], ratings = [9, 7, 8, 6], threshold = 8.
- i=0: rating 9 ≥ 8 → add "Dune"
- i=1: rating 7 ≥ 8? No
- i=2: rating 8 ≥ 8 → add "Cosmos"
- i=3: rating 6 ≥ 8? No
Returns ["Dune", "Cosmos"] ✓
PART B — recommend
Algorithm: Track the index of the maximum rating seen so far. Initialize maxIndex = 0. Iterate from index 1 through the end of ratings. If the current rating is strictly greater than the rating at maxIndex, update maxIndex. (Using strictly greater than ensures ties are broken by earliest occurrence.) After the loop, save the title at maxIndex, then remove the element at maxIndex from both titles and ratings. Return the saved title.
public String recommend(ArrayList<String> titles, ArrayList<Integer> ratings) {
int maxIndex = 0;
for (int i = 1; i < ratings.size(); i++) {
if (ratings.get(i) > ratings.get(maxIndex)) {
maxIndex = i;
}
}
String topTitle = titles.get(maxIndex);
titles.remove(maxIndex);
ratings.remove(maxIndex);
return topTitle;
}Trace with example: titles = ["Dune", "1984", "Cosmos"], ratings = [9, 7, 9].
- maxIndex starts at 0 (rating 9)
- i=1: rating 7 > 9? No
- i=2: rating 9 > 9? No (tie — maxIndex stays 0)
topTitle = "Dune". Remove index 0 from both lists → titles = ["1984", "Cosmos"], ratings = [7, 9]. Returns "Dune" ✓
Teacher Explanation
Part A tests the ability to traverse two parallel ArrayLists simultaneously using a standard index-based for loop and selectively populate a new list — a core AP CSA skill. Students must use >= (not >) for the threshold comparison and must not modify the original lists.
Part B tests finding the index of a maximum value in a list, then using that index to remove corresponding elements from two parallel lists. The key subtlety is using strictly greater than when updating maxIndex so that ties resolve to the earliest occurrence. Students must remove from both lists using the same index, and must save the title before removing it.
Education Coach Advice
AP Exam Strategy:
- For parallel-list problems, always use an index-based
forloop so you can access both lists at the same index simultaneously. - When building a filtered list, create the new
ArrayListat the top of the method and return it at the bottom — don't forget thereturnstatement. - For "find and remove the max" problems, find the index first, save what you need, then remove. Never remove inside the search loop.
- Trace your code with the given example before moving on — it takes 30 seconds and catches most off-by-one and operator errors.
- Remember:
ArrayList.remove(int index)removes by position; make sure you're not accidentally callingremove(Object).
Traps
Part A traps:
- Using
>instead of>=for the threshold check — would exclude books rated exactly at the threshold. - Modifying the original
titleslist instead of building a new one. - Using an enhanced for-each loop over
titleswithout tracking the index, making it impossible to look up the corresponding rating.
Part B traps:
- Using
>=instead of>when updatingmaxIndex— this would cause ties to resolve to the last occurrence rather than the first. - Forgetting to remove the element from one of the two lists.
- Removing from
titlesfirst and then trying to remove fromratingsat the same index — this is actually fine since both use the same savedmaxIndex, but students who re-calltitles.indexOf(...)after removal may get the wrong index. - Not saving the title string before calling
remove().

