Skip to main content

AP CSA FRQ Guide

2017 FRQ 3: Phrase

Modify a Phrase class by replacing a specific occurrence of a target string and finding the last occurrence of a target.

String Search and Replacement22 minute targetStringindexOfsubstringloopsedge cases

Skills Tested

What this FRQ is really practicing

StringindexOfsubstringloopsedge cases

Review these topics before attempting the problem.

Starter Approach

How to begin with your own plan

  1. 1Part A: call the provided findNthOccurrence(str, n) helper and update the phrase only when the returned index is not -1.
  2. 2Part B: call findNthOccurrence with increasing occurrence numbers, preserving the last valid index and returning -1 when no occurrence exists.

Attempt the problem before opening the solution.

Common Mistakes

Mistakes to watch for while writing

Replacing the first occurrence instead of the nth occurrence.
Continuing the next search from the same index and finding the same occurrence again.
Changing the phrase when the nth occurrence does not exist.
Reimplementing the search rather than using the required findNthOccurrence helper.

Self Check

Review questions before you submit

Uses the provided findNthOccurrence helper.

Both parts require findNthOccurrence calls for full credit; do not reimplement it with indexOf.

Tracks which occurrence has been found.

The nth occurrence is not always the first occurrence.

Rebuilds the phrase using substring pieces.

Strings are immutable, so replacement must create a new string.

Handles missing occurrences safely.

findNthOccurrence returns -1 when that occurrence does not exist.

Practice Links

Where to go next