Two Pointers Template: Pattern, Code & Cheat Sheet
The Two Pointers pattern is one of the most frequently tested coding interview patterns. Use two pointers converging or diverging on sorted arrays or linked lists. This template gives you a reusable code skeleton, pseudocode, and implementation in multiple languages so you can solve 18+ problems using this single mental model.
Difficulty: Easy | Time Complexity: O(n) | Space Complexity: O(1)
When to Use This Template
Use the Two Pointers template when you see these signals in a problem:
Prerequisites: Arrays basics
Problem count on W Code: 18 problems across Easy, Medium, and Hard difficulty levels.
If the problem does not match these signals, consider alternative patterns.
Pseudocode Template
function twoPointerSolve(arr):
left = 0
right = length(arr) - 1
while left < right:
current = compute(arr[left], arr[right])
if current == target:
return result
else if current < target:
left += 1
else:
right -= 1
return not_foundPython Implementation
pythondef two_pointer_solve(arr: list, target: int) -> list: left, right = 0, len(arr) - 1 while left < right: current = arr[left] + arr[right] if current == target: return [left, right] elif current < target: left += 1 else: right -= 1 return [-1, -1]
Java Implementation
javapublic int[] twoPointerSolve(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left < right) { int sum = arr[left] + arr[right]; if (sum == target) return new int[]{left, right}; else if (sum < target) left++; else right--; } return new int[]{-1, -1}; }
C++ Implementation
cppvector<int> twoPointerSolve(vector<int>& arr, int target) { int left = 0, right = arr.size() - 1; while (left < right) { int sum = arr[left] + arr[right]; if (sum == target) return {left, right}; else if (sum < target) left++; else right--; } return {-1, -1}; }
Variations & Adaptations
The Two Pointers pattern has several variations you should master:
Variation 1: Converging Pointers (sorted array)
This variation is useful when the problem specifically requires converging pointers (sorted array). Adapt the main template by modifying the core loop/recursion logic accordingly.
Variation 2: Fixed Separation (linked list)
This variation is useful when the problem specifically requires fixed separation (linked list). Adapt the main template by modifying the core loop/recursion logic accordingly.
Variation 3: Expanding from Center (palindrome)
This variation is useful when the problem specifically requires expanding from center (palindrome). Adapt the main template by modifying the core loop/recursion logic accordingly.
Variation 4: In-place Modification (remove duplicates)
This variation is useful when the problem specifically requires in-place modification (remove duplicates). Adapt the main template by modifying the core loop/recursion logic accordingly.
Common Mistakes & Edge Cases
When implementing Two Pointers, watch out for:
Edge cases to always test:
Step-by-Step Problem Solving Guide
Frequently Asked Questions
What problems can I solve with the Two Pointers template?
What is the time complexity of Two Pointers?
What should I learn before Two Pointers?
How do I recognize a Two Pointers problem in an interview?
Practice 18+ Two Pointers problems on W Code with instant feedback and AI-powered hints. Start your free practice now!
Start Learning Free