Two Pointers & Sliding Window
In short: some problems scan an array with one loop nested inside another — O(n²) — but really only need two indexes moving through it together, each one only ever going forward. That's the two-pointer technique: one pointer at each end closing inward, or two pointers (read and write, or window start and end) moving the same direction. Every step discards work for good, so the whole pass is O(n) time and usually O(1) extra space.

Fill a 9-minute gap with two tracks
The obvious way tries every pair — O(n²):
The inner loop learns nothing from the sort. Once 2 + 8 is too big, every
pair with the 8 is too big too — 2 is the shortest partner it will ever get.
Put lo on the shortest track and hi on the longest instead: too small →
move lo right; too big → move hi left; exact → done. Each comparison
throws one track away for good, so at most n steps — O(n) time, O(1)
space:
Each comparison throws away one track for good, which is why the pair search takes at most n steps.
The mistake: trying this on unsorted data. Pointers from both ends only
work because the sort guarantees "too big" means everything from here to
hi is too big. Sort first (O(n log n) — still beats O(n²)), or keep the
original positions by sorting indexes, not values.
mins = [1, 3, 4, 5, 9]
lo, hi = 0, len(mins) - 1
steps = 0
while lo < hi:
steps += 1
total = mins[lo] + mins[hi]
if total == 8:
break
if total < 8:
lo += 1
else:
hi -= 1
print(mins[lo], mins[hi], steps)The same "walk inward" shape reverses a list and checks palindromes — but both ends must start on real items:
IndexError"level" reads the same both ways, so this should print True. It crashes on the very first comparison instead.