Skip to main content
Interview

Two pointer technique masterclass - all patterns in one place

Aditi KhannaAditi Khanna
8/14/2025
00

Two pointers is probably the most versatile technique. Here's every pattern I've seen:

Opposite ends:

  • Two sum (sorted array)
  • Container with most water
  • Valid palindrome
  • 3Sum / 4Sum

Same direction (fast/slow):

  • Remove duplicates from sorted array
  • Linked list cycle detection
  • Move zeroes
  • Remove element

Merge pattern:

  • Merge sorted arrays
  • Intersection of two arrays
  • Merge intervals (sort + pointer)

The key insight: if the problem involves a sorted array/list and mentions O(1) space, think two pointers first.


two-pointerspatternstutorialdsa

Comments (7)

Sign in to join the discussion.
Rishabh Garg
Rishabh Garg6 months ago

Just curious, can two pointers be applied effectively to unsorted lists?

Hari Rastogi
Hari Rastogi10 months ago

I like the fast/slow pointer pattern for cycle detection. Do you have any resources to practice these?

Ali Petrov
Ali Petrov10 months ago

I've seen 'Linked list cycle detection' in some interviews. Does the two pointers technique always guarantee O(n) time complexity for this?

Ritesh Pandey
Ritesh Pandey11 months ago

Awesome summary! But I'm struggling with the '3Sum' problem. Any tips on how to think about the two pointers here?

Sachin Ahuja
Sachin Ahuja11 months ago

Wish I had this post when I was prepping for my interviews. Would you say two pointers is more important for arrays than other types?

Omar Weber
Omar Weber8/23/2025

Is 'Container with most water' really using two pointers in the most efficient way?

Vera Chen
Vera Chen8/22/2025

I get confused with merge intervals. Does the two-pointer technique actually simplify this pattern?