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.
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).
Two-pointer vocabulary.
Used in symmetric-comparison problems.
- 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.
How two-pointer is tested.
Termination condition and short-circuit return dominate.
- 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.
Test "racecar" and "hello".
Two-pointer trace.
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.
Two-pointer pitfalls.
Mostly bounds and operators.
- 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.
Two-pointer template.
Initialization, condition, update.
Two-Pointer Reference
AP Quick Reference| Part | Value | Note |
|---|---|---|
| left init | 0 | Start of string. |
| right init | length - 1 | End of string. |
| Loop condition | left < right | Stop at middle. |
| Update | left++; right-- | Move toward middle. |
How to master two-pointer.
Drill palindrome variations.
- 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.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 12 Topic 02 String Building and Two-Pointer Comparison FRQ Practice
AP-style topic practice assessment