AP CSA 2015 FRQ 3 Sparsearray

FRQ Archive2015 FRQs › FRQ 3: SparseArray
2015 AP CSA • Interface Implementation

AP CSA 2015 FRQ 3: SparseArray

Classic Exam Note: This FRQ originally involved implementing an interface. Interface implementation is no longer tested on the current AP CSA exam, but the ArrayList manipulation and class design skills here are fully applicable today.

Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF

9 Points Hard Interface Implementation Classic Format (Pre-2019)
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

Download Full 2015 FRQ 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) */
    }
}
Timer 22:00

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.

Drag corner to expand ▽

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;
}
Linear search through entries. Return the value immediately on match. Return 0 (the default for absent entries in a sparse array) only after the loop completes without a match.

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).

Drag corner to expand ▽

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--;
}
Two things must happen: (1) remove entries in the target column (backward traversal for safe removal), and (2) decrement the column index of entries in columns to the RIGHT of the removed column. Don't forget numCols--.

Common Mistakes to Avoid

Forgetting to decrement column indices of surviving entries

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.

Forgetting numCols--

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

SparseArrayEntry is immutable — to change a column value, create a new SparseArrayEntry and use entries.set(i, newEntry).
The backward traversal handles both operations (remove and adjust) safely in one pass.
Note: Interface implementation context (this class may implement an interface) is no longer tested on the current exam, but the ArrayList manipulation here is directly applicable.

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.

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]