Two Pointer Technique Explained for Interviews

The two pointer technique — when to use it, how it works, and the most common interview problems it solves.

What is the two pointer technique?

The two pointer technique uses two indices (pointers) that traverse a data structure — usually an array or string — to solve problems in O(n) time that would otherwise require O(n²). Instead of checking every possible pair with nested loops, two pointers move strategically to eliminate impossible candidates.

This technique appears in roughly 15 to 20 percent of coding interview problems. It is one of the first patterns you should learn because it is simple to implement, easy to reason about, and frequently tested at every level from intern to senior.

Three variations of two pointers

Opposite-direction pointers

One pointer starts at the beginning, the other at the end, and they move toward each other. This works on sorted arrays or symmetric structures. The classic example is Two Sum II on a sorted array: if the sum of the values at the two pointers is too large, move the right pointer left; if too small, move the left pointer right.

Other examples: valid palindrome (compare characters from both ends), container with most water (move the shorter side inward), and trapping rain water (track left and right maximums converging to the center).

Same-direction pointers (fast and slow)

Both pointers start at the beginning but move at different speeds. The fast pointer scans ahead while the slow pointer tracks a result position. Use this for in-place array modification: remove duplicates from a sorted array, move zeroes to the end, or remove a specific element.

For linked lists, this variation detects cycles (Floyd's cycle detection): the slow pointer moves one step, the fast pointer moves two steps. If they meet, there is a cycle. To find the cycle start, reset one pointer to the head and move both one step at a time — they meet at the cycle entry.

Sliding window pointers

Technically a special case of two pointers, the sliding window uses a left and right pointer to maintain a window over a contiguous subarray or substring. The right pointer expands the window, and the left pointer contracts it when a constraint is violated. This is covered in depth in our sliding window guide.

When to recognize a two pointer problem

Look for these signals: the input is sorted or can be sorted without losing information; you are looking for pairs or subarrays that satisfy a condition; the brute force involves nested loops over the same array; or the problem asks for in-place modification with O(1) extra space.

If the problem says "find two numbers that sum to a target" and the array is sorted, two pointers is almost certainly the answer. If the array is unsorted, a hash map is usually better. Recognizing which tool fits is more important than knowing the tools themselves.

Worked example: Three Sum

Given an array of integers, find all unique triplets that sum to zero. The brute force is O(n³) with three nested loops. The two pointer solution reduces it to O(n²).

First, sort the array. Then iterate through each element as the first number. For each first number, set a left pointer to the next element and a right pointer to the end of the array. If the three-number sum is too large, move right left. If too small, move left right. If equal, record the triplet and move both pointers, skipping duplicates.

The key insight: sorting enables the two pointer approach, and skipping duplicates avoids repeated triplets. This is a pattern you will see in Four Sum and other k-sum variants.

Worked example: remove duplicates in-place

Given a sorted array, remove duplicates in-place and return the new length. The slow pointer tracks where to write the next unique element. The fast pointer scans through the array. When the fast pointer finds a value different from the slow pointer's value, increment slow and copy the value.

After the loop, slow + 1 is the length of the deduplicated array. This technique works because the array is sorted — all duplicates are adjacent. The same pattern applies to removing a specific value or partitioning an array.

Practice problems

Start with these in order: Two Sum II (sorted input), valid palindrome, remove duplicates from sorted array, container with most water, three sum, and trapping rain water. Each builds on the previous one. After these six problems, you will recognize two pointer opportunities instinctively.

When practicing, tools like Phantom Coder can confirm whether your solution is optimal and suggest the right pointer direction if you get stuck.

Common mistakes

Forgetting to handle duplicates in problems that ask for unique results. Using two pointers on an unsorted array when the technique requires sorted input. Moving the wrong pointer — always think about which direction eliminates candidates. And off-by-one errors on the initial positions: left usually starts at 0 (or i+1), right at array.length - 1.

Ready to ace your next interview?

Phantom Coder gives you real-time AI assistance, invisible to screen sharing.

Download for macOS — $99/mo