Career OS

Learn · DSA · Pattern 2

Two Pointers

Instead of one finger scanning a list, you use two — and let them work toward each other. It turns a bunch of O(n²) problems into O(n), often with zero extra memory.

Before we start

📋 What you’ll learn
  • What the two-pointer pattern is and the two flavours it comes in
  • How it solves “find a pair” on a sorted array with no extra memory
  • The exact signals that scream “use two pointers”
  • How it compares to the hashmap approach — and when each wins
✅ After this you’ll be able to
  • Solve palindrome, pair-sum, and “remove in place” problems
  • Choose two pointers vs a hashmap on purpose (time vs space)
  • Explain an O(n) time, O(1) space solution out loud

Why you’re learning it: it’s one of the highest-frequency interview patterns, and it teaches a key idea — when data is sorted, you can often skip the hashmap entirely and save memory. ⏱️ ~25 min. Do Arrays & Hashing and Big-O first.

What is the two-pointer pattern?

Imagine a sorted row of numbers. Put one finger on the far left (smallest) and one on the far right (largest). Now you have a dial: moving the left finger right makes your total bigger; moving the right finger left makes it smaller. Because the list is sorted, each move is a smart, informed step — never a blind scan.

The two flavours

↔️ Converging (opposite ends)

Left starts at 0, right at the end; they move toward each other. Used for: pair-sum on sorted arrays, palindrome checks, “container with most water”.

🐇🐢 Fast & slow (same direction)

Both start at the left; one moves faster. Used for: removing duplicates in place, detecting a loop in a linked list, finding the middle.

Watch it work — pair sum on a sorted array

Find two numbers in [1, 3, 5, 8, 12] that add to 9. L = left pointer, R = right pointer.

1 0 L
3 1
5 2
8 3
12 4 R

sum = 1 + 12 = 13. Too big (need 9) → move the RIGHT pointer left to shrink it.

1 0 L
3 1
5 2
8 3 R
12 4

sum = 1 + 8 = 9. That’s the target! Answer = indices [0, 3]. ✅

No nested loop, no hashmap — just two pointers converging. O(n) time, O(1) extra space.

When to reach for it — the triggers

A sorted array + “find a pair”Palindrome / read from both endsReverse or swap in placeRemove duplicates in placeCompare / merge two sequences

Where you’ll use it — real life

🔁 Merging two sorted feeds

Walking a bank file and a ledger together with two pointers is a classic reconciliation move — line them up without loading everything into a map.

🔤 Palindrome / validation

Checking a string reads the same both ways is literally a left pointer and a right pointer marching inward.

🧹 Cleaning data in place

Removing duplicates or blanks from a list without a second array — fast pointer reads, slow pointer writes.

💧 Optimisation problems

“Container with most water”, “3-sum” — all built on the converging-pointer idea.

Two Pointers vs a Hashmap

Hashmap (Pattern 1)Two Pointers
Needs sorting?NoYes (or already sorted)
Extra memoryO(n) — stores what it’s seenO(1) — just two indices
Best whenUnsorted data, need counts/lookupsSorted data, want to save memory

Same problem, two tools. Knowing why you’d pick each is exactly the judgement interviews probe.

Why we practice this

The pattern is simple; recognising when it applies is the skill. A few reps and “sorted array + find a pair” instantly lights up as “two pointers”.

Now YOU do the reps

🗣️ The 2-minute explain test

Out loud: “How do two converging pointers find a pair in a sorted array, and why is it O(1) space when a hashmap is O(n)?” Then log it in your Journal.


Back to your Siemens roadmap →

Saves your progress on this device.