Two Pointer Pattern
Fast recall
Quick revision
Core answer
Whether you understand Two Pointer Pattern beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The converging pattern — palindrome check, executed:
func isPalindrome(s string) bool {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
if s[i] != s[j] { return false }
}
return true
}
// isPalindrome("racecar") → true — verified
// isPalindrome("hello") → false — verifiedThe pattern: two pointers starting at both ends, moving toward each other. Each iteration compares, then advances. The loop ends when they cross. O(n) time, O(1) space.
When two-pointer wins: the data is ORDERED (sorted, or symmetric like a palindrome) and the answer is a pair or a position — the ordering lets you decide which pointer to move from the comparison, skipping n-1 pairs per step.
The three variants:
- 1Converging (i from start, j from end) — palindrome, sorted two-sum (if the array is sorted: too small → i++, too big → j--)
- 2Fast/slow (same direction, different speeds) — cycle detection (they meet), middle-finding (fast ends, slow is mid)
- 3Dual-array (one pointer per array) — sorted merge, intersection
The trade vs. the hash map: two-sum on an UNSORTED array with no ordering — hash map O(n) time, O(n) space. Two-sum on a SORTED array — two-pointer O(n) time, O(1) space. The sortedness buys the space saving.
Two Pointer Pattern
Direct Answer
Two indices traversing an array (or two arrays) — converging (from both ends toward center), same-direction (fast/slow), or from two starts. O(n) where nested loops would be O(n²).
Deep Dive & Architecture
The pattern's correctness argument (why you can skip): for the converging two-sum on sorted data — arr[i] + arr[j] < target means arr[i] can't pair with ANYTHING at or after j (all would be smaller) — so i++ is safe. Each step eliminates a range, not just a pair — that's the O(n) despite the two pointers.
The fast/slow cycle detection (Floyd's tortoise and hare): slow moves 1, fast moves 2 — if there's a cycle, fast laps slow (they meet); if not, fast reaches nil. The subtle part: they meet INSIDE the cycle, not necessarily at the entry — the follow-up (finding the entry point) is the k=distance-from-head-to-entry trick.
The pattern's limits: it requires the ability to ORDER the decisions (sorted, symmetric, or sequential). On unordered data with pair-target questions, the hash map is the correct O(n) — the two-pointer degenerates to O(n²) without the ordering to exploit.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Two Pointer Pattern in a real project?
- How would you compare Two Pointer Pattern with an alternative?
- What is a common mistake when using Two Pointer Pattern?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.