01
Overview

Two pointers — one from each end.

Whether reversing a string or testing if it's a palindrome, two pointers moving toward each other handle both tasks elegantly.

The two-pointer pattern uses two indices — typically left starting at 0 and right starting at length - 1 — that move toward each other through a String. At each step, the chars at both positions are compared or used.

Two-pointer comparison powers palindrome detection. Building a reversed String works similarly: peel from the end and append, or use a single loop with concat.

On the AP exam, palindromes and reverse FRQs are classic two-pointer territory.

02
Core Concept

Move toward the middle until they cross.

Stop when left >= right.

Palindrome check

public boolean isPalindrome(String s) {
    int left = 0;
    int right = s.length() - 1;
    while (left < right) {
        if (s.charAt(left) != s.charAt(right)) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}

If any pair mismatches, the String isn't a palindrome — short-circuit return false. If the loop completes, every paired char matched.

Reverse a String (iterative)

public String reverseString(String s) {
    String result = "";
    for (int i = s.length() - 1; i >= 0; i--) {
        result += s.charAt(i);
    }
    return result;
}

Building from the end forward produces the reverse.

Termination

For an even-length string, left and right end up adjacent then crossed. For an odd-length string, they meet at the middle char (which doesn't need to be compared with itself).

03
Key Vocabulary

Two-pointer vocabulary.

Used in symmetric-comparison problems.

Vocabulary
  • Two-pointer — two indices moving through one structure.
  • Left pointer — starts at 0; advances.
  • Right pointer — starts at length - 1; retreats.
  • Palindrome — string equal to its reverse.
  • Symmetric pairing — pairing chars equidistant from each end.
  • Convergence — pointers meeting in the middle.
04
AP Exam Focus

How two-pointer is tested.

Termination condition and short-circuit return dominate.

Exam Focus
  • Loop while left < right. Stops at the middle.
  • Return false on first mismatch. Short-circuit.
  • Use == on chars. Primitives — not .equals().
  • Empty/single-char strings. Trivially palindromes — loop never runs; return true.
05
Worked Example

Test "racecar" and "hello".

Two-pointer trace.

Worked Example AP-style reasoning

isPalindrome("racecar"):

  • left=0 ('r'), right=6 ('r'): match.
  • left=1 ('a'), right=5 ('a'): match.
  • left=2 ('c'), right=4 ('c'): match.
  • left=3, right=3: loop exits (left < right false).

Returns: true.

isPalindrome("hello"):

  • left=0 ('h'), right=4 ('o'): mismatch → return false.
06
Common Mistakes

Two-pointer pitfalls.

Mostly bounds and operators.

Watch Out
  • Starting right at length. Off by one; out of bounds.
  • Using <= in loop. Causes the middle char to compare to itself unnecessarily.
  • Using .equals() on chars. Chars are primitives.
  • Forgetting to advance both pointers. Infinite loop.
  • Returning true too early. Must verify all pairs before claiming palindrome.
07
Reference Table

Two-pointer template.

Initialization, condition, update.

Two-Pointer Reference

AP Quick Reference
PartValueNote
left init0Start of string.
right initlength - 1End of string.
Loop conditionleft < rightStop at middle.
Updateleft++; right--Move toward middle.
08
Practice Tip

How to master two-pointer.

Drill palindrome variations.

Practice Strategy
  • MCQ Practice — trace short strings; check the loop bound and short-circuit return.
  • Java Lab — implement isPalindrome, reverseString, hasSameEnds. Test even and odd lengths.
  • FRQ Practice — start with left = 0, right = length - 1; while (left < right) as muscle memory.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 12 Topic 02 String Building and Two-Pointer Comparison FRQ Practice

AP-style topic practice assessment

Start