AP CSA 2015 FRQ 3 Sparsearray
AP CSA 2015 FRQ 3: SparseArray
Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF
| Question Type | Interface Implementation |
| Key Skills | Implement getValueAt() (search an ArrayList of SparseArrayEntry objects for a given row/column, return the value or 0 if |
| Difficulty | Hard |
| AP CSA Units | Unit 4: Data Collections (ArrayList search, backward traversal for removal, index updating). |
What This Problem Asks
Implement getValueAt() (search an ArrayList of SparseArrayEntry objects for a given row/column, return the value or 0 if not found) and removeColumn() (remove all entries in a given column AND decrement the column field of all surviving entries that were in higher columns).
Official Question & PDF
PDF cannot display here. Open PDF directly →
Provided Code
public class SparseArrayEntry
{
public SparseArrayEntry(int r, int c, int v) {
/* given */
}
public int getRow() {
/* given */
}
public int getCol() {
/* given */
}
public int getValue() {
/* given */
}
}
public class SparseArray
{
private int numRows, numCols;
private ArrayList entries;
public int getValueAt(int row, int col) {
/* part (a) */
}
public void removeColumn(int col) {
/* part (b) */
}
}
Part A — getValueAt (3 Points)
Write getValueAt(). Return the value of the entry at (row, col), or 0 if no entry exists at that position.
Scoring Rubric
| +1 | Iterates over entries |
| +1 | Checks getRow() and getCol() for both matching |
| +1 | Returns getValue() on match; returns 0 if no match found |
Solution
public int getValueAt(int row, int col)
{
for (SparseArrayEntry e : entries)
{
if (e.getRow() == row && e.getCol() == col)
{
return e.getValue();
}
}
return 0;
}
Part B — removeColumn (6 Points)
Write removeColumn(). Remove all entries in column col. Then decrement the column value of all remaining entries that were in a column greater than col (since those columns shift left by one).
Scoring Rubric
| +1 | Iterates over entries list |
| +1 | Removes entries where getCol() == col |
| +1 | Uses backward traversal (or equivalent) to avoid index issues |
| +1 | Decrements column value for entries with getCol() > col |
| +1 | Updates numCols by decrementing by 1 |
| +1 | Algorithm: correctly handles both removal and column adjustment |
Solution
public void removeColumn(int col)
{
for (int i = entries.size() - 1; i >= 0; i--)
{
SparseArrayEntry e = entries.get(i);
if (e.getCol() == col)
{
entries.remove(i);
}
else if (e.getCol() > col)
{
entries.set(i, new SparseArrayEntry(
e.getRow(), e.getCol() - 1, e.getValue()));
}
}
numCols--;
}
Common Mistakes to Avoid
Wrong
// Only removes entries in col — doesn't adjust entries in col+1, col+2, ...
Correct
else if (e.getCol() > col) { entries.set(i, new SparseArrayEntry(r, c-1, v)); }
When column col is removed, all entries originally in columns col+1, col+2, ... must shift left by one. Forgetting this adjustment corrupts the array representation.
Wrong
// Removes entries and adjusts columns but numCols still reports wrong size
Correct
numCols--; // must decrement after removing the column
numCols must reflect the actual number of columns after removal. Forgetting to decrement it leaves the array reporting a stale column count.
Exam Tips
Frequently Asked Questions
What does 2015 AP CSA FRQ 3 SparseArray ask?
It asks students to write getValueAt() (find an entry at a specific row/column or return 0) and removeColumn() (remove all entries in a given column, then decrement the column value of all entries in higher columns and update numCols).
Why does removeColumn need to update column indices?
A sparse array represents a 2D grid. When a column is removed, all columns to its right logically shift left by one. Entries that were in column k (where k > removed col) must update their stored column value to k-1 to remain correctly positioned in the grid.
See All 2015 AP CSA FRQs
View the complete 2015 exam hub with solutions, difficulty analysis, and scoring for all four questions.
View 2015 FRQ Hub →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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]