โ† Coding Interview Prep (LeetCode) learning path
Coding Interview Prep (LeetCode) ยท Module 3 of 10

Arrays and two pointers

Two pointers move through an array to avoid nested loops.

๐Ÿ“– Key ideas

  1. On a sorted array, pointers at both ends can find a pair with a target sum in O(n).
  2. Fast and slow pointers remove duplicates in place.
  3. Reversing an array swaps from both ends toward the middle.
  4. Prefix sums answer range-sum questions in O(1) after O(n) setup.
  5. In-place changes save extra memory.
๐Ÿ’ก Real-life example: Like two people searching a sorted bookshelf from opposite ends and meeting in the middle.
Two pointersTwo indexes moving through data
Prefix sumRunning total array for range sums
In placeChanging data without extra arrays
Sorted inputData in increasing order