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 Garg8 months ago

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

Hari Rastogi
Hari Rastogi10/8/2025

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

Ali Petrov
Ali Petrov10/7/2025

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 Pandey9/6/2025

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

Sachin Ahuja
Sachin Ahuja9/4/2025

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?