compare_arrowsTwo Pointers

Two Pointers: when to spot it, explain it, and practice it

Two pointers is the pattern for problems where relative movement matters more than remembering every previous state. It is especially strong when order, symmetry, or deduplication drives the answer.

Pattern coverage

60+

Best first move

Ask whether the pointers move in the same direction or opposite directions.

Common failure point

Moving the wrong side without a proof tied to ordering.

When this pattern should come to mind

The answer depends on comparing two positions and deciding which side should move.
The array is sorted, partially ordered, or can be sorted first.
You need deduplication, partitioning, or in-place compaction.

Checklist before you code

Ask whether the pointers move in the same direction or opposite directions.
Name the invariant before moving either pointer.
If duplicates exist, decide exactly where deduplication happens.
Do not move both pointers blindly unless the invariant proves it is safe.

The solving flow that works well in interviews

1

Write down what each pointer means before you move them.

2

Use the current comparison to prove which side can be safely discarded.

3

Move exactly one side unless the problem explicitly requires both.

4

Handle duplicates right where they would create repeated work or repeated answers.

5

Re-check the loop condition to avoid skipping the final valid pair.

Common variants

Opposite-direction

Great for sorted arrays, maximizing area, or converging on a target sum.

Fast/slow pointers

Useful for cycle detection, in-place filtering, and linked-list traversal.

Partition pointers

Useful when elements need to be grouped by a predicate or pivot.

Template preview

PythonPublic preview
# Opposite-direction pointers on a sorted array
left, right = 0, len(nums) - 1
while left < right:
    if good(nums[left], nums[right]):
        return answer
    if should_move_left(nums[left], nums[right]):
        left += 1
    else:
        right -= 1

# Fast/slow pointers
slow = 0
for fast in range(len(nums)):
    if keep(nums[fast]):
        nums[slow] = nums[fast]
        slow += 1

A more useful problem ladder for practice

This is not a random list. It is ordered to help candidates build recognition first, add key variants next, and then increase pressure with harder cases.

High-frequency mistakes

warning

Moving the wrong side without a proof tied to ordering.

warning

Forgetting duplicate skipping in 3Sum-style problems.

warning

Using two pointers where a hash map is actually cleaner.

warning

Missing edge cases around equal elements and pointer crossing.

Recommended practice path

1

Start with Two Sum II and Container With Most Water.

2

Then solve 3Sum and deduplication-heavy array problems.

3

Add linked-list fast/slow pointer questions afterward.

4

Finish with harder partition and cycle-detection hybrids.

Two Pointers Pattern Guide | LeetCode Interview Prep - Interview AiBox