Two Pointers on a Sorted Array
Move ends inward to meet a target.
Two Pointers on a Sorted Array is a free Coding Interview Prep lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Two Pointers
The two-pointer technique scans an array with two indices instead of nested loops, turning many O(n^2) ideas into a single clean O(n) pass. 🎯
Sorted Is the Magic Word
The classic version needs a sorted array. Order lets you reason: moving right grows the value, moving left shrinks it, so each step is a real decision.
Two Pointers at the Ends
Start one pointer at the left end and one at the right end. They face each other and will slowly close the gap between them.
left = 0
right = len(a) - 1Move Ends Inward
On each step you move exactly one pointer inward. The array's order tells you which side to nudge to get closer to your goal.
The Loop Condition
Keep looping while left < right. When they meet or cross, every useful pair has been checked and you can stop.
while left < right:
# inspect a[left] and a[right]
passReading the Current Sum
Look at a[left] + a[right] as your current candidate. Comparing it to a target tells you whether you need a bigger or smaller value next.
total = a[left] + a[right]Too Small: Move Left
If the sum is below the target, you need more. Move the left pointer right toward larger values, since the array is sorted ascending.
if total < target:
left += 1Too Big: Move Right
If the sum is above the target, you need less. Move the right pointer left toward smaller values to bring the total down.
elif total > target:
right -= 1Each Step Throws Away Work
Every move eliminates a whole batch of pairs you never need to test. That is why the scan is linear instead of quadratic.
Why It Stays Correct
You only discard pairs that cannot match, so the right answer is never skipped. This safety is what makes two pointers trustworthy in contests.
Beyond the Endpoints
The same idea powers variants: reversing in place, partitioning, and merging. Master the meeting pointers and these all feel familiar.
Quick Check
You are scanning a sorted array from both ends for a target sum.
Recap
Two pointers sweep a sorted array from both ends, moving one inward each step while left < right. Linear, correct, and the base for many tricks. 🚀
Frequently asked questions
Is the “Two Pointers on a Sorted Array” lesson free?
Yes — the full text of “Two Pointers on a Sorted Array” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Two Pointers on a Sorted Array”?
Move ends inward to meet a target. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Two Pointers on a Sorted Array” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Two Pointers on a Sorted Array
- Find a Pair with a Given Sum
- Remove Duplicates In Place
- Merge Two Sorted Sequences