Two Pointers

Abstract Idea:

  • Use two pointers (or indices) to traverse data from one or both ends, often to find a condition involving pairs or subsequences.

Pattern:

  • You’re comparing elements from two places (beginning & end, or two sequences).
  • You’re told to find pairs or triplets.
  • Problem involves sorted data or linked list.

Problems:


String: Unequal length handling

Abstract Idea:

  • One part of the input runs out before the other — the solution must gracefully handle incomplete data or imbalanced structure.

Pattern:

  • You’re merging, comparing or aligning multiple structures of different sizes.
  • One side might finish early or the strings “may not be the same length”.

Problems: