Concept
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.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Two pointers doing its job.
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 NoneApplying 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.
Your roadmap puts these before Two pointers, unless you already know them.
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.
Topics that build on Two pointers.