Back to Blog
Coding PatternsArraysDSAInterview Tips

Mastering the Two Pointers Technique (with Patterns & Code)

Amit Singh

Amit Singh

Author

June 25, 2026
9 min read

Two pointers is the pattern that collapses a nested-loop O(n²) scan into a single O(n) pass by walking two indices instead of restarting the inner loop. You reach for it when a brute-force solution re-scans the array, and the data has structure you can exploit — usually sorted order or a notion of contiguous progress. Learn the three variants below and you'll recognize it on sight.

When to reach for it (recognition cues)

  • The brute force is "for each element, scan the rest" (O(n²)) and you suspect O(n) is possible.
  • The array/string is sorted (or can be), and you're looking for a pair/triple meeting a condition.
  • You need an in-place rearrangement (dedupe, move zeros, partition).
  • You're working with a linked list and need cycle detection or the middle node.

The three variants

1. Opposite-end pointers (converging)

Start one pointer at each end and move them toward each other based on a condition. The classic: find a pair summing to a target in a sorted array.

def two_sum_sorted(nums, target):
    lo, hi = 0, len(nums) - 1
    while lo < hi:
        s = nums[lo] + nums[hi]
        if s == target:
            return [lo, hi]
        if s < target:   # need a bigger sum -> move left pointer up
            lo += 1
        else:            # need a smaller sum -> move right pointer down
            hi -= 1
    return []

The key insight: sortedness lets each comparison eliminate one whole direction, so you never backtrack — O(n) time, O(1) space. Same idea powers Container With Most Water and Valid Palindrome.

2. Fast & slow pointers (different speeds)

Two pointers over the same structure moving at different speeds — the tool for linked-list cycle detection (Floyd's algorithm) and finding the middle node.

def has_cycle(head):
    slow = fast = head
    while fast and fast.next:
        slow = slow.next          # +1
        fast = fast.next.next     # +2
        if slow is fast:
            return True           # they meet inside a cycle
    return False

If there's a cycle, the fast pointer laps the slow one and they meet; if not, fast falls off the end.

3. Same-direction read/write (in-place)

A "write" pointer trails a "read" pointer to compact an array in place — remove duplicates from a sorted array, move zeros to the end, etc. (When the same-direction pair bounds a contiguous range you grow and shrink, it's the sliding window — a specialized two-pointers case.)

Worked recognition example

"Given a sorted array, find if any two numbers sum to K." Brute force is O(n²). Spotting "sorted + pair + condition" → opposite-end pointers → O(n). That recognition step is the actual interview skill; the code is short once you've named the variant.

Complexity

Two pointers typically takes O(n) time, O(1) extra space — the whole point is replacing a nested loop with a single coordinated pass.

Common mistakes

  • Forgetting to sort (or assuming sorted when it isn't) before using converging pointers.
  • Wrong pointer-move logic — be explicit about which condition advances which pointer.
  • Off-by-one / infinite loops — get the while lo < hi boundary right and ensure each branch moves a pointer.
  • Reaching for it on unsorted pair-sum when a hash map is the better O(n) tool — two pointers shines when sorted or in-place.

Practice problems

Two Sum II (sorted), Valid Palindrome, Container With Most Water, 3Sum, Remove Duplicates from Sorted Array, Move Zeroes, Linked List Cycle, Middle of the Linked List.

Internalize the three variants and the recognition cues, then drill them — build a schedule with our free study-plan generator, or get live feedback in our DSA course.

More interview patterns: Sliding Window · Binary Search · Dynamic Programming · Graph Algorithms

Written by Amit Singh — Senior SDE at Amazon, Claude Certified Architect, and founder of AlgoEngineer.

Ready to Ace Your Interviews?

Join thousands of students who have successfully landed their dream jobs at FAANG companies.