Two Pointer Technique Java | Fresher Interview
Fast recall
Quick revision
Core answer
Pattern recognition — knowing when a problem's structure allows two pointers instead of brute-force nested loops
Not realizing two-end pointers usually require sorted input, or confusing the two-end variant with the fast/slow variant
Name 3-4 concrete problems and explain the invariant each maintains, like 'slow tracks the last unique position'
Interview-ready guide
Interview answer
The two-pointer technique uses two indices that move through an array or string based on problem conditions.
It's called two-pointer because you literally maintain two integer variables, like left and right, and move them independently. The core idea is to eliminate nested loops by making smart decisions about which direction to move each pointer. There are two main variants.
The two-end variant starts pointers at opposite ends and moves them inward. This is perfect for sorted array problems like finding a pair that sums to a target. If the current sum is too small, you move left rightward to get bigger values.
If it's too large, you move right leftward. Each move eliminates an entire range of pairs, not just one, so you scan the whole space in O of n time instead of O of n squared. The same-direction variant has both pointers starting at the beginning but moving at different speeds or conditions.
Think of a slow pointer tracking where valid elements should go, and a fast pointer scanning ahead to find them. This is how you remove duplicates from a sorted array in place. The sorted-input requirement matters for the opposite-end sum pattern, and duplicate skipping matters in problems such as three-sum.
The variants are easiest to separate by their invariant: opposite-end pointers discard impossible ranges, while a slow pointer can mark the last valid write position as a fast pointer scans.
Two Pointer Technique Java | Fresher Interview
Direct Answer
The two-pointer technique uses two indices moving through data — typically one at each end or both from the same start — based on conditions. It reduces many O(n^2) brute-force problems to O(n) by replacing nested loops, on sorted or otherwise structured data.
Code Example
// TWO-END: find a pair summing to target in a SORTED array
static int[] pairSum(int[] a, int target) {
int left = 0, right = a.length - 1;
while (left < right) {
int sum = a[left] + a[right];
if (sum == target) return new int[]{a[left], a[right]};
if (sum < target) left++; // need bigger -> move left right
else right--; // need smaller -> move right left
}
return new int[]{-1, -1};
}
// SAME-DIRECTION: remove duplicates from a SORTED array in place
static int dedup(int[] a) {
int slow = 0;
for (int fast = 1; fast < a.length; fast++)
if (a[fast] != a[slow]) a[++slow] = a[fast];
return slow + 1; // new logical length
}
Common Mistakes & Pitfalls
- Forgetting to sort first: two-end pointers need sorted input to make directional elimination decisions. If the input is unsorted but sorting is allowed, sort it.
- Not handling duplicates: in problems like three-sum, skip duplicate values or you get duplicate results.
- Confusing the variants: using fast-slow for pair-sum, or two-end for in-place filtering — each solves a different class of problem.
Check your understanding
Answer these without looking back at the article.
- How do you solve three-sum using two pointers?
- How do you remove duplicates from a sorted array in-place?
- What is the container with most water problem?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.