Two Pointers Technique
Master the Two Pointers technique! Visualize how moving start and end pointers can check whether a string is a palindrome in linear time — one of the most common patterns for array and string problems, and a must-know for FAANG interviews.
Two Pointers: Valid Palindrome
Master the Two Pointers technique! Visualize how moving start and end pointers can check whether a string is a palindrome in linear time — one of the most common patterns for array and string problems, and a must-know for FAANG interviews.
Initialize pointers at start (0) and end (n-1)
Initialize pointers at start (0) and end (n-1)
Palindrome check
Order the steps of the two-pointer palindrome check.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
When is the Two Pointers technique most applicable?
Write the code
Valid Palindrome
The short version
Imagine two people walking towards each other from opposite ends of a long table to meet in the middle. By scanning from both ends, they inspect the whole table much faster than one person running back and forth.
In more depth
Two Pointers: Core Patterns
Two Pointers uses two integer indices to traverse a data structure, cutting O(n²) brute-force to O(n). Five distinct patterns cover the majority of interview problems.
1. Opposite-Ends (Sorted Array)
Left starts at 0, right at n−1. If the running value (sum, product, area) is too small advance left; too large retreat right. Requires a sorted array. Classic problems: Pair Sum, 3Sum (fix one element then run this on the rest), Container With Most Water, Trapping Rain Water.
2. Same-Direction: Slow-Write / Fast-Read
Both pointers move left-to-right at different rates. The fast pointer scans every element; the slow "write" pointer records the next kept position. Used for: Remove Duplicates In-Place, Move Zeroes to End.
3. Same-Direction: Fixed Gap (n-step Lead)
Advance the lead pointer n steps first, then move both together at the same speed. When the lead reaches the end, the trailing pointer is exactly n positions from the end. Used for: Nth-from-End Node in a linked list.
4. Fast / Slow — Tortoise and Hare
Both move forward, slow by 1 step and fast by 2. If a cycle exists they will eventually meet; if the fast pointer reaches null, the list is acyclic. Also finds the middle of a list (stop when fast reaches the end).
5. Palindrome / Symmetry
Left at index 0, right at n−1; compare and move both inward. The first mismatch proves not a palindrome. O(n) time, O(1) space — no string copy needed. This is the pattern this visualization demonstrates.
Choosing the right variant: Array sorted, need a pair? → Opposite-Ends. Filter or build in-place with no extra space? → Slow-Write / Fast-Read. Linked list cycle or midpoint? → Tortoise & Hare. Symmetry check on a string or array? → Palindrome pattern.
When to reach for this
- Finding a pair of elements that sum to X in a sorted array
- Checking for Palindromes
- In-place array manipulation (Remove Duplicates)
- Container/Water problems
Interview Questions
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required