Visualized DSA Strategies

Episode 4 Two Pointers

1 h 3 min · JavaScript · Watch on YouTube · Quiz

Do you need this episode?

4 quick questions on what it teaches. Get them all right and skip ahead; miss one and you'll see the part of the video that answers it.

The complete guide to two pointers for coding interviews, in JavaScript. The four shapes (both ends, read and write, two arrays, fast and slow) and the rule that says which pointer moves; what each approach costs; the JavaScript template for each; and five traps. Then nine types, each worked as a small problem in full: examples, the brute force animated beside the running line of code, the complete code, every line explained, and the two-pointer solution the same way.

What you'll learn

  • Why two pointers turn a double loop into one pass
  • Both ends on sorted data: too big moves r, too small moves l
  • A palindrome check that skips punctuation, in O(1) space
  • Moving the shorter wall: the container with the most water
  • 3Sum: sort, fix one number, two pointers on the rest
  • Read and write pointers to filter an array in place
  • Merging from the back so nothing unread is overwritten
  • Three zones to sort 0, 1 and 2 in one pass
  • Fast and slow pointers to find a repeat, and trapping rain water from both ends

Chapters

Quiz: test yourself

9 questions: the ones from the end of the video, then new code. Pick an answer to see why it's right.