If you are preparing for a software engineering interview in 2026, the two pointers pattern is one of the highest-return techniques you can learn. It turns many brute-force O(n²) solutions into clean O(n) ones, uses constant extra space, and shows up in screens at companies of every size. This guide explains when to reach for it, the three main variants, and how to talk through your solution so interviewers see your reasoning, not just your code.

What Is the Two Pointers Technique?
Two pointers means keeping two indices into a sequence (an array, string, or linked list) and moving them according to a rule, so that each element is visited a bounded number of times. Instead of checking every pair with nested loops, you use structure in the data, usually sorted order or symmetry, to decide which pointer to move.
It is distinct from the sliding window pattern, although the two overlap. Two pointers focuses on relationships between two positions (a pair, a partition, a mirror); sliding window focuses on a contiguous range with a running state.
How to Recognize a Two Pointers Problem
Look for these signals in the problem statement:
- The input is sorted, or you are allowed to sort it.
- You need to find a pair, triplet, or subset meeting a target condition.
- The problem mentions a palindrome, reversal, or symmetric comparison.
- You must modify an array in place with O(1) extra space (remove duplicates, move zeroes, partition).
- The input is a linked list and you need the middle, a cycle, or the nth node from the end.
Variant 1: Opposite Ends (Converging Pointers)
Place one pointer at the start and one at the end, then move them toward each other. The classic example is Two Sum II on a sorted array.
public int[] twoSum(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) return new int[]{left + 1, right + 1};
if (sum < target) left++; // need a larger sum
else right--; // need a smaller sum
}
return new int[]{-1, -1};
}
The key insight to explain aloud: because the array is sorted, a sum that is too small can only be fixed by moving the left pointer up, and a sum that is too large only by moving the right pointer down. No valid pair is ever skipped, which is why a single pass is enough. Time is O(n), space is O(1).
Problems to practice
- Valid Palindrome and Palindrome Linked List
- Container With Most Water
- 3Sum and 4Sum (sort, fix one element, converge on the rest)
- Trapping Rain Water

Variant 2: Fast and Slow Pointers
Both pointers start at the same place but move at different speeds. This is the standard approach for linked list problems and for detecting cycles without extra memory.
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
If a cycle exists, the fast pointer eventually laps the slow one, just as a faster runner catches a slower one on a circular track. The same idea finds the middle node (when fast reaches the end, slow is at the midpoint), the start of a cycle (Floyd’s algorithm), and the happy number problem.
Variant 3: Same Direction (Read and Write Pointers)
Here both pointers move forward, but one reads and one writes. It is the go-to pattern for in-place array rewriting.
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int write = 1;
for (int read = 1; read < nums.length; read++) {
if (nums[read] != nums[write - 1]) {
nums[write++] = nums[read];
}
}
return write;
}
The invariant is easy to state: everything before write is the finished result. Stating invariants clearly is one of the strongest signals of seniority in an interview. The same structure solves Move Zeroes, Remove Element, and Sort Colors (the three-pointer Dutch national flag variant).

A Step-by-Step Interview Framework
- Clarify the input. Is it sorted? Can it contain duplicates or negatives? Can you modify it in place?
- State the brute force first. A nested-loop O(n²) answer shows you understand the problem and gives you a baseline to improve.
- Name the observation that enables two pointers. Usually sorted order, symmetry, or a monotonic property.
- Define what each pointer means and what rule moves it.
- Walk through a small example before coding, including an edge case.
- Code, then test with empty input, a single element, all duplicates, and no valid answer.
- Close with complexity. Usually O(n) time (O(n log n) if you sorted) and O(1) space.
Common Mistakes to Avoid
Off-by-one and loop conditions
Using left <= right when you need left < right (or the reverse) causes a pointer to pair with itself. Decide up front whether an element may be used twice.
Forgetting to skip duplicates
In 3Sum, failing to skip repeated values produces duplicate triplets. After sorting, advance past equal neighbors for the fixed element and after recording a match.
Applying it to unsorted data
Converging pointers only work when moving a pointer has a predictable effect. If the data is unsorted and you must preserve indices, a hash map is usually the better tool, and it is worth saying so out loud.
Not explaining why pointers are safe to move
Interviewers care about the proof that you never discard a valid answer. In Container With Most Water, for example, you move the shorter wall because keeping it can never produce a larger area with a narrower width.
A Two-Week Practice Plan
Consistency beats cramming. Try this schedule of 60 to 90 minutes a day:
- Days 1 to 3: Converging pointers on easy problems (Two Sum II, Valid Palindrome, Reverse String).
- Days 4 to 6: Read/write pointers (Remove Duplicates, Move Zeroes, Sort Colors).
- Days 7 to 9: Fast and slow pointers (cycle detection, middle of list, Happy Number).
- Days 10 to 12: Medium problems: 3Sum, Container With Most Water, Trapping Rain Water.
- Days 13 to 14: Timed mock sessions where you explain your thinking out loud and re-solve missed problems from memory.

Key Takeaways
Two pointers is less about memorizing problems and more about spotting structure: sorted order, symmetry, or a need for in-place updates. Learn the three variants, practice stating invariants, and always compare against the brute-force baseline. Do that consistently and a large share of array, string, and linked list questions become routine.
Start Practicing Today
Pick three problems from the lists above and solve them out loud this week, timing yourself and writing down the invariant for each. If you want a place to rehearse realistic interview questions, you can also try Niraswa AI. Your next interview is closer than it feels, so start your preparation now.
