Three Sum Java Two Pointers | Fresher Interview
Fast recall
Quick revision
Core answer
target - fixed. Skip duplicate values to avoid duplicate triplets. This is O(n^2) time.Whether you can combine sorting with two pointers and handle the duplicate-skipping edge case that trips up many candidates
Not skipping duplicates after finding a triplet (duplicate results), or using a HashMap which makes duplicate handling harder
Clearly explain the duplicate-skipping at both the outer loop and the inner pointers — show you've implemented it
Interview-ready guide
Interview answer
To solve three-sum using two pointers, you first sort the array, then use an outer loop to fix one element at index i, and apply two pointers on the rest to find pairs that sum to negative nums at i.
This reduces the problem from O of n cubed brute force to O of n squared. First, sort the array with Arrays dot sort. This costs O of n log n but it's necessary to enable two-pointer search.
Then loop i from zero to nums dot length minus two. For each i, initialize left at i plus one and right at the last index. The target is negative nums at i because you want the three elements to sum to zero.
While left is less than right, check the sum of nums at left plus nums at right. If it equals target, you found a triplet, add it to your result. If the sum is less than target, increment left to get a larger sum.
If it's greater, decrement right. Duplicate handling is the part that keeps the result correct. You need to skip duplicates in three places.
In the outer loop, if i is greater than zero and nums at i equals nums at i minus one, continue to skip the same fixed element. After finding a valid triplet, use while-loops to skip duplicate left values and duplicate right values before moving the pointers. These must be while-loops, not if statements, because you might have several consecutive duplicates.
Skipping at the anchor, left pointer, and right pointer ensures that each value triplet appears only once.
Three Sum Java Two Pointers | Fresher Interview
Direct Answer
Sort the array, fix one element with an outer loop, then apply two pointers on the remaining sorted subarray to find pairs summing to target - fixed. Skip duplicate values to avoid duplicate triplets. This is O(n^2) time.
Deep Dive & Architecture
Sorting turns the three-number search into a sequence of two-number searches. For each anchor index i, the remaining pair must total -nums[i]. A left pointer starts after the anchor and a right pointer starts at the end.
Worked example: [-1, 0, 1, 2, -1, -4]
After sorting, the array is [-4, -1, -1, 0, 1, 2]. Anchor -4 finds no pair totaling 4. With anchor -1 at index 1, left is the next -1 and right is 2; their sum is 1, so record [-1, -1, 2]. Move inward and skip repeated values. The next useful pair is 0 + 1 = 1, which records [-1, 0, 1].
The anchor at index 2 is another -1, so skip it; otherwise the same triplets would be generated again. After recording a triplet, skip equal values at both pointers before continuing. These three duplicate checks—anchor, left, and right—make the result unique.
Sorting costs O(n log n). The outer loop runs O(n) times and each pointer pair crosses the remaining range once, so O(n²) dominates. Apart from the result list and sorting implementation, the pointer search uses O(1) extra space.
Code Example
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ThreeSumSolver {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if (nums == null || nums.length < 3) return result;
Arrays.sort(nums); // O(n log n)
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicate anchor
int left = i + 1, right = nums.length - 1, target = -nums[i];
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
result.add(List.of(nums[i], nums[left], nums[right]));
while (left < right && nums[left] == nums[left + 1]) left++; // skip dups
while (left < right && nums[right] == nums[right - 1]) right--;
left++; right--;
} else if (sum < target) left++;
else right--;
}
}
return result;
}
}
// nums = {-1,0,1,2,-1,-4} -> [[-1,-1,2], [-1,0,1]]
Common Mistakes & Pitfalls
- Forgetting to skip duplicates: the number one mistake — without skipping, the result contains duplicate triplets.
- Skipping with
ifinstead ofwhile: three consecutive duplicates still leak through; use while-loops. - Using a HashMap instead of sorting: leads to complex nested maps and harder duplicate handling.
- Off-by-one in the outer loop: loop
i < nums.length - 2soleftandrighthave room.
Check your understanding
Answer these without looking back at the article.
- What is the two-pointer technique and when do you use it?
- How do you check if a string is a palindrome using two pointers?
- How do you remove duplicates from a sorted array in-place?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.