Back to blog

Two Pointers Problems You Must Know

3 March 20262 min read

Opposite Ends (Sorted Array)

Two pointers at start and end; move based on comparison with target.

Classic problems

  • Two Sum (sorted).
  • Three Sum: fix one, two pointers for the rest.
  • Container with most water.
  • Valid palindrome.

Same Direction (Sliding Window Style)

Both pointers move forward; one may "lag" to represent the start of a valid segment.

Examples

  • Remove duplicates in-place.
  • Remove element in-place.
  • Move zeros to end.

Fast and Slow Pointers

Used in linked lists (and sometimes arrays): one pointer moves twice as fast. Great for cycle detection and finding the middle.

Examples

  • Linked list cycle detection (Floyd).
  • Find middle of linked list.
  • Linked list cycle II (find cycle start).

In-Place and Partitioning

  • Partition array (e.g. Dutch national flag): multiple pointers to partition into regions.
  • Sort colors.

Summary

Two pointers reduce time from O(n²) to O(n). Identify "pairs," "subarray," or "cycle" to choose the variant. Use Preplume(/) to practice.

Related posts

Common DSA Mistakes and How to Avoid Them

Typical mistakes in coding interviews: off-by-one errors, wrong complexity, and how to avoid them.

14 Mar 20262 min readRead more