Sign in to track progress and bookmarks.
The Two Pointers pattern uses two indices to traverse a data structure (usually an array or linked list) in tandem. It is the most common technique for solving "In-Place" array problems with O(1) Space.
Example: "Is a string a Palindrome?" or "Reverse an array." You have a pointer at the Start and a pointer at the End. They move towards each other until they meet. This allows you to process the array in exactly N/2 steps.
Example: "Remove duplicates from a sorted array." One pointer tracks the 'last unique element found' and the other pointer scans ahead for new values. This allows you to 'Compress' the array in a single pass without extra memory.
Q: "What is the advantage of Two Pointers over a simple nested loop?"
Architect Answer: "Time complexity. A nested loop for 'sum of two numbers' would be **O(N^2)**. A Two Pointer approach on a sorted array is **O(N)**. Since you only pass through the data once, it is significantly more scalable for large datasets. It also demonstrates an understanding of pointer arithmetic and memory efficiency."
Quizzes linked to this course—pass to earn certificates.
On this page
1. Opposite Direction (Meeting in the middle) 2. Same Direction (Fast and Slow) 4. Interview Mastery