Lesson 13 of 30
A plain-language walk through the two-pointer idea, using two people starting at opposite ends of a line and meeting in the middle.
9 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Picture a line of people at a bus stop. You want to know if the line looks the same read from the front as it does read from the back. One way: stand at the front, walk the whole line, memorize every face, then compare.
Here's a faster way. Send one person to stand at the very front and another to stand at the very back. Both walk toward the middle at the same time, checking: "does my person match your person?" The moment they meet in the middle, you're done — and you never had to write anything down.
That's the two-pointer idea. Two positions ("pointers") start at opposite ends of a list and move toward each other, one step at a time, comparing as they go. Instead of one long walk with your hands full of notes, it's two short walks that meet halfway.
If "pointer" sounds like it should mean something scary and technical, it doesn't. It just means "a position you're keeping track of." You already do this when you hold your place in two different pages of a book with two fingers.
A palindrome is a sequence that reads the same forwards and backwards, like the numbers [1, 2, 3, 2, 1]. Here's how two pointers check that, using a short array of numbers.
function isPalindrome(numbers) {
// "left" starts at the first spot, "right" starts at the last spot.
let left = 0;
let right = numbers.length - 1;
// Keep walking toward each other until they meet or cross.
while (left < right) {
// If the two numbers being compared don't match, it's not a palindrome.
if (numbers[left] !== numbers[right]) {
return false;
}
// They matched, so step both pointers one position closer to the middle.
left++;
right--;
}
// The pointers met (or crossed) without ever finding a mismatch.
return true;
}
console.log(isPalindrome([1, 2, 3, 2, 1])); // true
console.log(isPalindrome([1, 2, 3, 4, 5])); // false
Trace it by hand: left starts at 1, right starts at the last 1 — they match. left moves to 2, right moves to 2 — they match. Now left and right are both pointing at 3, so the loop stops. No mismatch was ever found, so it's a palindrome.
left <= right instead of left < right. When left and right land on the exact same spot, there's nothing left to compare — that one middle element doesn't need checking against itself. Stopping there avoids comparing a number to itself."racecar" — the pointers don't care what's stored at each position, only whether the two sides match.This is the simple, walking-toward-each-other version of two pointers. The JavaScript track has a lesson on more advanced two-pointer patterns — like finding a pair of numbers in a sorted list that add up to a target — that builds directly on this same "two positions moving with intent" idea, just applied to trickier problems.