Skip to main content

Two Pointers

The Two Pointers technique is a foundational coding interview pattern used to solve array and string problems efficiently. Instead of using nested loops, you move two indices in a coordinated way, often reducing time complexity from O(n²) to O(n) while not adding any extra memory.

If you recognize this pattern early in a problem, you can often jump straight to an optimal solution.


What Is the Two Pointers Technique?

two pointers mental model

Two pointers means:

  • You maintain two indices into a data structure (usually an array or string)
  • You move one or both pointers either starting at different positions and moving toward each other, or moving in the same direction based on a condition

The key insight is that pointer movement is intentional, not exploratory.


When Should You Use Two Pointers?

You should immediately consider two pointers when the problem involves:

  • Arrays or strings
  • Pair comparisons
  • In-place modification
  • Sorted data (very common)
  • Constraints like O(1) extra space

If you see “find two”, “check pairs”, “remove duplicates”, or “reverse”, this pattern should come to mind.


Core Two Pointer Patterns (with Pseudocode)

Pattern 1: Opposite Ends (Left & Right)

Description

  • One pointer starts at the beginning
  • One pointer starts at the end
  • The pointers move inward based on a comparison

Common use cases

  • Valid Palindrome
  • Reverse array or string

Pseudocode Template

left = 0
right = n - 1

while left < right:
if condition_met(nums[left], nums[right]):
return result

if need_smaller_value:
right -= 1
else:
left += 1

Mental model

Shrink the search space from both ends.


Pattern 2: Same Direction (Fast & Slow)

Description

  • Both pointers start at the beginning
  • One pointer moves faster than the other
  • Often used to overwrite values in-place

Common use cases

  • Remove duplicates
  • Detect cycles

Pseudocode Template

slow = 0

for fast in range(0, n):
if valid(nums[fast]):
nums[slow] = nums[fast]
slow += 1

Mental model

Fast scans, slow builds the result.


Pattern 3: Sliding Window (Two Pointers Variant)

Description

  • Two pointers define a dynamic window
  • One pointer expands the window
  • The other shrinks it when constraints break

Common use cases

  • Longest substring
  • Subarrays with constraints
  • Frequency-based problems

Pseudocode Template

left = 0

for right in range(0, n):
add(nums[right])

while window_invalid:
remove(nums[left])
left += 1

update_answer()


Example Walkthrough: Two Sum (Sorted Array)

Problem

Given a sorted array, return whether two numbers sum to a target.

Step-by-Step Logic

  1. Start one pointer at the beginning
  2. Start one pointer at the end
  3. Compare their sum to the target
  4. Move the pointer that brings you closer to the target

Pseudocode

left = 0
right = n - 1

while left < right:
sum = nums[left] + nums[right]

if sum == target:
return true
else if sum < target:
left += 1
else:
right -= 1

return false

two sum

Why This Works

  • Increasing left increases the sum
  • Decreasing right decreases the sum
  • Each step eliminates impossible pairs

Time Complexity: O(n)

Space Complexity: O(1)


Why Interviewers Love Two Pointers

Using two pointers correctly shows that you:

  • Recognize problem structure
  • Avoid brute force
  • Optimize time and space
  • Can reason about constraints

Many interview questions are designed to punish nested loops.


Common Mistakes to Avoid

1. Using Two Pointers Without a Monotonic Property

If moving a pointer doesn’t reliably improve the situation, the approach may be invalid.

2. Forgetting Sorted Input

Some two pointer solutions require sorted data.

Always confirm whether sorting is allowed.

3. Infinite Loops

Ensure:

  • At least one pointer moves every iteration
  • Loop conditions are correct (left < right)

How to Explain This in an Interview

A strong explanation sounds like:

“Because the array is sorted, I can use two pointers starting at both ends.

If the sum is too small, I move the left pointer; if it’s too large, I move the right pointer.

This allows me to solve it in linear time.”

Clear reasoning matters as much as correct code.


Key Takeaways

  • Two pointers replace nested loops with linear scans
  • They rely on directional logic
  • They often enable O(n) solutions
  • They are a core interview expectation

If you’re serious about coding interviews, two pointers should feel automatic.