Pattern 1: Two pointers
Comes up in: DSA
This concept is being written. It gets the full five steps — explanation, predict, interview angle, practice with tests, and review cards. Here is exactly what it will cover, so you can study it elsewhere in the meantime.
What it covers
- opposite-ends pointers on sorted input
- same-direction (read/write) pointers
- template
- when it applies
- proof sketch of why moving the smaller pointer is safe
Words you will know
What you will practise
- pair-sum-sorted (E)
- remove-duplicates-sorted (E)
- move-zeroes (E)
- sort-colors (M)
- trapping-rain-water (H)
- valid-palindrome-ii (E)
- three-sum (M)
- container-most-water (M)
Interview questions this answers
- When does the two-pointer technique work? Why must the input often be sorted?