Mid-Level Patterns 6 min read

Two Pointer Technique — Algorithm Pattern for Interviews

The Interview Question

What is the two pointer technique? Give examples of problems where it's useful.

Expert Answer

The two pointer technique uses two indices that move through a data structure (usually a sorted array or linked list) to solve problems in O(n) time that would otherwise require O(n²) with brute force. There are two main patterns. Opposite-direction pointers start at both ends and move inward — used for problems like two-sum on sorted arrays, container with most water, and palindrome checking. Same-direction pointers (fast/slow) both start at the beginning with one moving faster — used for removing duplicates in-place, detecting cycles in linked lists (Floyd's algorithm), and finding the middle of a linked list. The key insight: two pointers work when there's a monotonic relationship — as one pointer moves, you can determine which direction the other should move based on the current state. This eliminates the need to check every pair.

Key Points to Hit in Your Answer

  • Reduces O(n²) brute force to O(n) by eliminating redundant comparisons
  • Requires sorted data (for opposite-direction) or sequential structure (for same-direction)
  • Opposite-direction: two-sum (sorted), palindrome check, container with most water
  • Same-direction (fast/slow): remove duplicates, linked list cycle detection, find middle node
  • Three pointers: used for Dutch National Flag (sort 0s, 1s, 2s)
  • Works because of monotonic relationship: moving one pointer eliminates invalid pairs

Code Example

# Two Sum on sorted array — opposite direction
def two_sum_sorted(nums, target):
    left, right = 0, len(nums) - 1
    while left < right:
        current = nums[left] + nums[right]
        if current == target:
            return [left, right]
        elif current < target:
            left += 1    # need larger sum
        else:
            right -= 1   # need smaller sum

# Remove duplicates in-place — same direction
def remove_duplicates(nums):
    if not nums: return 0
    slow = 0
    for fast in range(1, len(nums)):
        if nums[fast] != nums[slow]:
            slow += 1
            nums[slow] = nums[fast]
    return slow + 1  # length of unique portion

# Linked list cycle detection — Floyd's
def has_cycle(head):
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

What Interviewers Are Really Looking For

The interviewer wants to see you recognize the pattern. When given a sorted array problem, your first thought should be 'can I use two pointers?' Walk through the logic of why you move left vs right. Floyd's cycle detection is a classic — know that you can find the cycle start by resetting one pointer to head and moving both at speed 1.

Practice This Question with AI Grading

Reading about interview questions is a start — but practicing with real-time AI feedback is how you actually get better. Goliath Prep grades your answers instantly and tells you exactly what you're missing.

Start Practicing Free →