1. Introduction to the Two Pointers Pattern
The Two Pointers technique utilizes two coordinate indices to traverse one or more data structures simultaneously, drastically reducing time or space complexity.
+─────────────────────────────────────────────────────────────────────────────+
| TWO POINTERS CONFIGURATIONS |
+─────────────────────────────────────────────────────────────────────────────+
1. Converging (Opposite Direction):
Left ───> [ 1, 2, 4, 6, 8, 11 ] <─── Right
2. Fast & Slow (Same Direction):
Slow ──> [ 1, 1, 2, 3, 3, 4 ]
Fast ─────────>
2. Core Interview Questions & Verified Solutions
Q1. 3Sum (Converging Pointers Pattern)
Problem Statement: Given an integer array nums, return all triplets [nums[i], nums[j], nums[k]] such that i ≠ j, i ≠ k, j ≠ k and nums[i] + nums[j] + nums[k] == 0. The solution set must not contain duplicate triplets.
pythondef threeSum(nums: list[int]) -> list[list[int]]: nums.sort() result = [] for i in range(len(nums) - 2): # Skip duplicate values for first element if i > 0 and nums[i] == nums[i - 1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total == 0: result.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left + 1]: left += 1 # Skip duplicate lefts while left < right and nums[right] == nums[right - 1]: right -= 1 # Skip duplicate rights left += 1 right -= 1 elif total < 0: left += 1 else: right -= 1 return result
- Time Complexity: O(N²) | Space Complexity: O(1) auxiliary space.
Q2. Container With Most Water
Problem Statement: Given an integer array height representing vertical lines on the x-axis, find two lines that together with the x-axis form a container containing the most water.
cpp#include <vector> #include <algorithm> int maxArea(const std::vector<int>& height) { int left = 0, right = height.size() - 1; int maxWater = 0; while (left < right) { int width = right - left; int currentHeight = std::min(height[left], height[right]); maxWater = std::max(maxWater, width * currentHeight); // Greedily move the shorter line inward if (height[left] < height[right]) { left++; } else { right--; } } return maxWater; }
- Time Complexity: O(N) | Space Complexity: O(1)
Q3. Trapping Rain Water
Problem Statement: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
javapublic int trap(int[] height) { if (height == null || height.length == 0) return 0; int left = 0, right = height.length - 1; int leftMax = 0, rightMax = 0; int totalWater = 0; while (left < right) { if (height[left] < height[right]) { if (height[left] >= leftMax) { leftMax = height[left]; } else { totalWater += leftMax - height[left]; } left++; } else { if (height[right] >= rightMax) { rightMax = height[right]; } else { totalWater += rightMax - height[right]; } right--; } } return totalWater; }
- Time Complexity: O(N) | Space Complexity: O(1)
3. Two Pointers Pattern Summary
| Problem | Pointer Direction | Movement Trigger |
|---|---|---|
| Two Sum Sorted | Opposite | If sum < target: left++; else right-- |
| 3Sum | Opposite inside loop | Deduplicate consecutive identical numbers |
| Container Most Water | Opposite | Always advance the shorter wall |
| Trapping Rain Water | Opposite | Process the side with smaller maximum boundary |
Practice all Two Pointers coding questions on DSAMaster's practice platform.
