1. Concept library
  2. Data Structures & Algorithms
  3. Module 2: Arrays and strings

Concept

Two pointers

Two indexes walk a sequence from the ends or at different speeds, so a pair or a span is found in one pass instead of two nested loops. It works whenever moving one pointer rules out a set of candidates.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain why moving one pointer rules out a set of candidates
  2. Solve a pair-sum problem on a sorted array with two pointers
  3. Spot the pattern applied to unsorted data where the argument breaks

In practice

A few lines that show Two pointers doing its job.

Python
def pair_with_sum(sorted_nums, target):
    lo, hi = 0, len(sorted_nums) - 1
    while lo < hi:
        total = sorted_nums[lo] + sorted_nums[hi]
        if total == target:
            return lo, hi
        if total < target:
            lo += 1
        else:
            hi -= 1
    return None

The common mistake

Watch for

Applying the ends-inward pattern to unsorted data. The argument that moving a pointer discards candidates only holds when the sequence is sorted; sort first or use a hash map.

What to know first

Your roadmap puts these before Two pointers, unless you already know them.

How it stays learned

Once a session shows you understand Two pointers, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Two pointers.

Next in the moduleSliding window →