2008 AP CSA FRQ 2: StringCoder - Solution

2008 AP CSA FRQ 2: StringCoder

Topic: String Manipulation & Encoding

Skills: substring operations, ArrayList traversal, string concatenation, encoding/decoding

Unit: Unit 2 (Selection/Iteration) (2025-2026 AP CSA)

Points: 9 | Time: ~22 minutes

Part (a)

Write the decodeString method that reconstructs a string from encoded StringPart objects.

Try First! Attempt before viewing solution.

Solution

public String decodeString(ArrayList parts)
{
    String decoded = "";
    for (StringPart sp : parts)
    {
        decoded += masterString.substring(
            sp.getStart(),
            sp.getStart() + sp.getLength());
    }
    return decoded;
}

Part (b)

Write the encodeString method that encodes a word using parts from the master string.

Solution

public ArrayList encodeString(String word)
{
    ArrayList parts = new ArrayList();
    int pos = 0;
    while (pos < word.length())
    {
        StringPart part = findPart(word.substring(pos));
        parts.add(part);
        pos += part.getLength();
    }
    return parts;
}

Key Concepts

✓ substring(start, end) is exclusive of end index
✓ Use helper method findPart when provided
✓ Build string incrementally using concatenation
✓ Track position in source string during encoding

Common Mistakes

Wrong substring indices (forgetting end is exclusive)
Not using the findPart helper method
Infinite loop if position not advanced
Off-by-one errors in substring calculation
Practice Similar: 2019 FRQ 1 | 2022 FRQ 1 | 2023 FRQ 1

Official Resources

Author: Tanner Crow - AP CS Teacher, 11+ years

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]