The Two Pointer Technique — Solve 20+ Interview Problems With One Pattern
Some coding interview problems look impossible at first glance.
The Two Pointer Technique — Solve 20+ Interview Problems With One Pattern
Some coding interview problems look impossible at first glance.
Then you learn the Two Pointer technique — and suddenly you can solve them in O(n) time with O(1) space, in minutes.
This is one of the highest-ROI patterns to learn for coding interviews.
The Core Idea
Instead of using nested loops (O(n²)), place two pointers at different positions in the array and move them strategically toward each other or in the same direction.
[1, 2, 3, 4, 5, 6, 7, 8]
↑ ↑
left right
Move them based on conditions. One pass. O(n) time.
Pattern 1 — Opposite Ends (Converging Pointers)
Two Sum — 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};
} else if (sum < target) {
left++; // need bigger sum → move left pointer right
} else {
right--; // need smaller sum → move right pointer left
}
}
return new int[]{-1, -1};
}
// [1, 2, 4, 6, 8, 10], target = 10
// left=0(1), right=5(10) → sum=11 > 10 → right--
// left=0(1), right=4(8) → sum=9 < 10 → left++
// left=1(2), right=4(8) → sum=10 == 10 → FOUND! ✅
Valid Palindrome
public boolean isPalindrome(String s) {
// Clean string first
String clean = s.toLowerCase().replaceAll("[^a-z0-9]", "");
int left = 0, right = clean.length() - 1;
while (left < right) {
if (clean.charAt(left) != clean.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
Container With Most Water (LeetCode 11)
public int maxWater(int[] height) {
int left = 0, right = height.length - 1;
int maxArea = 0;
while (left < right) {
int area = Math.min(height[left], height[right]) * (right - left);
maxArea = Math.max(maxArea, area);
// Move the shorter side inward (trying to find a taller wall)
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
Pattern 2 — Same Direction (Fast & Slow Pointers)
Remove Duplicates from Sorted Array
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int slow = 0; // points to last unique element
for (int fast = 1; fast < nums.length; fast++) {
if (nums[fast] != nums[slow]) {
slow++;
nums[slow] = nums[fast]; // write unique element
}
}
return slow + 1; // length of unique portion
}
// [1, 1, 2, 3, 3, 4]
// slow=0(1), fast scans → finds 2 → slow=1, nums[1]=2
// continues → finds 3 → slow=2, nums[2]=3
// continues → finds 4 → slow=3, nums[3]=4
// Result: [1, 2, 3, 4, ...] length=4 ✅
Detect Cycle in Linked List (Floyd’s Algorithm)
public boolean hasCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; // moves 1 step
fast = fast.next.next; // moves 2 steps
if (slow == fast) {
return true; // they met → cycle exists!
}
}
return false; // fast reached end → no cycle
}
Think of it like two runners on a circular track — the faster one will always lap the slower one if there’s a loop.
Pattern 3 — Three Pointers
3Sum — Find All Triplets That Sum to Zero
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i-1]) continue; // skip duplicates
int left = i + 1, right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (left < right && nums[left] == nums[left+1]) left++;
while (left < right && nums[right] == nums[right-1]) right--;
left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}
return result;
}
When to Use Two Pointers

🎯 Interview Tips
Q: Why sort before applying two pointers?
Sorting gives you predictable ordering — when the sum is too small, you know moving the left pointer right increases it. Without sorting, you can’t make this decision.
Q: What’s the difference between two pointers and sliding window?
Both use two indices, but sliding window maintains a window of elements and expands/contracts it. Two pointers typically converge toward each other or traverse in the same direction for comparison.
Q: What’s Floyd’s Cycle Detection used for beyond linked lists?
Finding cycles in sequences (like the “Happy Number” problem), finding the start of a cycle, and even in cryptography for finding collisions.
Key Takeaways
- Two pointers reduces O(n²) brute force to O(n)
- Converging: start from both ends, move toward center (palindrome, two sum)
- Same direction: slow/fast for in-place modification or cycle detection
- Always sort first when dealing with sums — enables the greedy pointer movement
- Recognizing this pattern is the key — once you see it, the code writes itself
Follow me for daily DSA & Java interview content. 🚀
DSA #Java #TwoPointers #CodingInterview #Algorithms
메타데이터
- post_id
- e20e243eded6
- slug
- the-two-pointer-technique-solve-20-interview-problems-with-one-pattern-e20e243eded6
- url
- https://medium.com/@vdhruval/the-two-pointer-technique-solve-20-interview-problems-with-one-pattern-e20e243eded6
- canonical_url
- https://medium.com/@vdhruval/the-two-pointer-technique-solve-20-interview-problems-with-one-pattern-e20e243eded6
- author_url
- https://medium.com/@vdhruval
- status
- ok
- fetched_at
- 2026-06-09 15:37:30