Two Pointers Coding Problems: 165 Questions with Solutions
165 two pointers coding problems — 61 easy · 90 medium · 14 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.
- Problems: 165
- By difficulty: 61 easy · 90 medium · 14 hard
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
- Cost: Free on every plan; sign in to run and submit
Two indices walking through an array — from both ends towards the middle, or one fast and one slow in the same direction — turn many quadratic scans into linear ones. Pair sums in a sorted array, removing duplicates in place, partitioning by a rule, comparing a string with its reverse, and the tortoise-and-hare cycle check are all the same move; these problems make it a reflex.
New to two pointers? Read the Two Pointers Technique tutorial first.
How two pointers works, step by step
nums = [1, 2, 4, 6, 8, 11, 15], target = 14- The array is sorted, so left starts at the smallest number and right at the largest. All 21 pairs are still possible, and each comparison will rule out a whole group of them at once.
- 1 + 15 = 16 is more than 14. 1 is the smallest number left, so 15 is too big with any partner: no pair can use it, and right steps left.
- right moves to 11, ruling out 6 pairs at once. 1 + 11 = 12 is less than 14. 11 is the largest number left, so 1 is too small with any partner, and left steps right.
- left moves to 2, ruling out 5 pairs at once. 2 + 11 = 13 is less than 14. 11 is the largest number left, so 2 is too small with any partner, and left steps right.
- left moves to 4, ruling out 4 pairs at once. 4 + 11 = 15 is more than 14. 4 is the smallest number left, so 11 is too big with any partner: no pair can use it, and right steps left.
- right moves to 8, ruling out 3 pairs at once. 4 + 8 = 12 is less than 14. 8 is the largest number left, so 4 is too small with any partner, and left steps right.
- left moves to 6, ruling out 2 pairs at once. 6 + 8 = 14: found, at indices 3 and 4. Every step threw one number out for good, so it took 6 comparisons instead of 21 — O(n) time, O(1) space.
Two Pointers study plan
14 of the 165 Two Pointers problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 151 in the full list below are practice at your own pace. Then move on to Sliding Window.
Day 1
Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.
- Reverse String Easy
- Valid Palindrome Easy
- Is Subsequence Easy
Day 2
Medium problems: the same pattern with one twist each. Name the twist before you code.
- Merge Strings Alternately Easy
- String Compression Medium
Day 3
More mediums. Before coding each one, write down what state the pattern keeps and when it changes.
- Two Sum II - Input Array Is Sorted Medium
- 3Sum Medium
Day 4
More mediums. Before coding each one, write down what state the pattern keeps and when it changes.
- Container With Most Water Medium
- Sort Colors Medium
Day 5
More mediums. Before coding each one, write down what state the pattern keeps and when it changes.
- Partition Labels Medium
- Interval List Intersections Medium
Day 6
Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.
- Trapping Rain Water Hard
Day 7
Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.
Day 8
Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.
Two Pointers: the essentials
When to reach for it
Sorted input (or input you may sort) and a question about pairs or triples with a target sum or difference; an in-place rewrite whose result is no longer than the input; two sequences to merge or compare in order; a palindrome check. In each, one comparison rules out a candidate for good, which is what lets two indices replace a nested loop.
The pattern
From opposite ends, lo = 0 and hi = n − 1: if the pair is too small, move lo right; if too big, move hi left. Each move discards every pair the moved pointer could still have made. In the same direction, a read pointer visits every element and a write pointer marks the end of the part being kept. When the pointers bound a range whose contents matter, it has become a Sliding Window.
def remove_duplicates(nums): # nums is sorted
write = 1 if nums else 0
for read in range(1, len(nums)):
if nums[read] != nums[write - 1]:
nums[write] = nums[read]
write += 1
return write
Cost
O(n) time after any sort and O(1) extra space. 3Sum is an outer loop around a two-pointer pass: O(n²), against O(n³) for three nested loops.
Common mistakes
- Opposite-end pointers on unsorted input, where a move no longer discards anything.
while lo <= hiwhen a pair needs two distinct elements; uselo < hi.- Skipping duplicates for one pointer only in 3Sum, so the same triple is reported twice.
- Moving both pointers when the comparison justified moving one.
Start with
- Valid Palindrome: two ends meeting in the middle.
- Container With Most Water: why moving the shorter side is safe.
- 3Sum: sort, fix one, two pointers for the rest.
All two pointers problems
Easy (61)
- Nth Node From End of Linked List Linked List
- Intersection of Two Sorted Linked Lists Linked List
- Middle of the Linked List Linked List
- Palindrome Linked List Linked List, Stack, Recursion
- Remove Duplicates from Sorted List Linked List
- Merge Two Sorted Lists Linked List, Recursion
- Two Sum IV - Input is a BST Tree, Depth-First Search, Hash Table
- Consecutive Characters String
- Positions of Large Groups String
- Valid Mountain Array Array
- Count the Number of Incremovable Subarrays I Array, Enumeration
- Shortest Word Distance Array, String
- Find Indices With Index and Value Difference I Array
- Pair With Given Difference Array, Sorting
- Shortest Distance to Target String in a Circular Array Array, String
- DI String Match Array, Greedy, String
- Two Sum Less Than K Array, Sorting
- Find the Distance Value Between Two Arrays Array, Binary Search
- Merge Two 2D Arrays by Summing Values Array
- Number of Distinct Averages Array, Hash Table, Sorting
- Valid Word Abbreviation String
- Reverse String II String
- Left Rotate an Array by D Places Array
- Common Elements in Three Sorted Arrays Array
- Find the Array Concatenation Value Array, Simulation
- Largest Positive Integer That Exists With Its Negative Array, Hash Table
- Find First Palindromic String in the Array Array, String
- Remove Palindromic Subsequences String
- Long Pressed Name String
- Reverse Only Letters String
- Union of Two Sorted Arrays Array
- Duplicate Zeros Array
- Sort Array By Parity II Array, Sorting
- Assign Cookies Array, Greedy, Sorting
- Flipping an Image Array, Matrix, Simulation
- Number of Arithmetic Triplets Array, Hash Table, Enumeration
- Apply Operations to an Array Array, Simulation
- Minimum Common Value Array, Hash Table, Binary Search
- Count Pairs Whose Sum is Less than Target Array, Binary Search, Sorting
- Check If N and Its Double Exist Array, Hash Table, Sorting
- Shortest Distance to a Character Array, String
- Reverse Prefix of Word String, Stack
- Merge Strings Alternately String
- Remove Element Array
- Count Binary Substrings String
- Reverse Vowels of a String String
- Reverse Words in a String III String
- Sort Array By Parity Array, Sorting
- Intersection of Two Arrays II Array, Hash Table, Sorting
- Intersection of Two Arrays Array, Hash Table, Sorting
- Happy Number Hash Table, Math
- Backspace String Compare String, Stack
- Squares of a Sorted Array Array, Sorting
- Valid Palindrome II String, Greedy
- Is Subsequence String, Dynamic Programming
- Find the Index of the First Occurrence in a String String
- Valid Palindrome String
- Merge Sorted Array Array, Sorting
- Remove Duplicates from Sorted Array Array
- Move Zeroes Array
- Reverse String String
Medium (90)
- Merge Two Sorted Linked Lists in Reverse Order Linked List, Sorting
- Rotate a Linked List Linked List
- Segregate Even and Odd Nodes in a Linked List Linked List
- Maximum Twin Sum of a Linked List Linked List, Stack
- Delete the Middle Node of a Linked List Linked List
- Swapping Nodes in a Linked List Linked List
- Merge In Between Linked Lists Linked List
- Odd Even Linked List Linked List
- Sort List Linked List, Sorting, Merge Sort
- Reorder List Linked List, Stack, Recursion
- Reverse Linked List II Linked List
- Partition List Linked List
- Remove Duplicates from Sorted List II Linked List
- Rotate List Linked List
- Remove Nth Node From End of List Linked List
- Maximize Distance to Closest Person Array
- Number of Ways Where Square of Number Is Equal to Product of Two Numbers Array, Hash Table, Math
- Find the Lexicographically Largest String From the Box I String, Enumeration
- Longest Uncommon Subsequence II Array, Hash Table, String
- Find Maximum Removals From Source String Array, Hash Table, String
- Maximum Total Damage With Spell Casting Array, Hash Table, Binary Search
- Minimize Maximum Pair Sum in Array Array, Greedy, Sorting
- Maximum Matching of Players With Trainers Array, Greedy, Sorting
- Range Sum of Sorted Subarray Sums Array, Binary Search, Sorting
- The Celebrity Problem Array, Stack, Graph
- Heaters Array, Binary Search, Sorting
- Count the Number of Fair Pairs Array, Binary Search, Sorting
- Shortest Subarray to be Removed to Make Array Sorted Array, Binary Search, Monotonic Stack
- Ways to Split Array Into Three Subarrays Array, Binary Search, Prefix Sum
- Append Characters to String to Make Subsequence String, Greedy
- Largest Merge Of Two Strings String, Greedy
- Expressive Words Array, String
- Maximize Greatness of an Array Array, Greedy, Sorting
- Divide Players Into Teams of Equal Skill Array, Hash Table, Sorting
- Partition Array According to Given Pivot Array, Simulation
- Find the Integer Added to Array II Array, Sorting, Enumeration
- Total Cost to Hire K Workers Array, Simulation, Heap (Priority Queue)
- Rotating the Box Array, Matrix, Simulation
- Separate Black and White Balls String, Greedy
- Find the Maximum Number of Marked Indices Array, Greedy, Sorting
- Maximum Number of Removable Characters Array, String, Binary Search
- Successful Pairs of Spells and Potions Array, Binary Search, Sorting
- Number of Subarrays with Bounded Maximum Array, Sliding Window
- Replace the Substring for Balanced String String, Sliding Window
- Sentence Similarity III String, Array
- Watering Plants II Array, Simulation
- Number of Subsequences That Satisfy the Given Sum Condition Array, Sorting, Binary Search
- The k Strongest Values in an Array Array, Sorting
- 3Sum With Multiplicity Array, Hash Table, Counting
- Most Profit Assigning Work Array, Greedy, Sorting
- Push Dominoes String, Dynamic Programming, Simulation
- Longest Mountain in Array Array, Dynamic Programming
- Partition Array Such That Maximum Difference Is K Array, Greedy, Sorting
- Sort Transformed Array Array, Math, Sorting
- Bag of Tokens Array, Greedy, Sorting
- Max Number of K-Sum Pairs Array, Hash Table, Sorting
- 4Sum Array, Sorting
- Maximum Distance Between a Pair of Values Array, Binary Search
- Sum of Square Numbers Math, Binary Search
- Minimum Length of String After Deleting Similar Ends String
- Longest Word in Dictionary Through Deleting String, Sorting
- Number of Matching Subsequences String, Binary Search
- Count Triplets With Sum Smaller Than X Array, Sorting
- Maximum Index Array
- Rearrange Array in Max/Min Form Array
- Subarray With Given Sum Array, Sliding Window
- Minimum Number of Swaps to Make the String Balanced String, Stack, Greedy
- Next Greater Element III Math, String
- Maximum Width Ramp Array, Stack, Monotonic Stack
- Minimum Number of Platforms Array, Greedy, Sorting
- Find K Closest Elements Array, Binary Search, Sorting
- Valid Triangle Number Array, Binary Search, Greedy
- Rearrange Array Elements by Sign Array, Simulation
- Longest String Chain Array, Hash Table, String
- Remove Duplicates from Sorted Array II Array
- String Compression String
- Reverse Words in a String String
- Shortest Unsorted Continuous Subarray Array, Sorting, Monotonic Stack
- Next Permutation Array
- 3Sum Closest Array, Sorting
- 3Sum Array, Sorting
- Interval List Intersections Array, Intervals
- Partition Labels String, Greedy, Hash Table
- Boats to Save People Array, Greedy, Sorting
- Sort Colors Array, Sorting
- Container With Most Water Array, Greedy
- Two Sum II - Input Array Is Sorted Array, Binary Search
- Compare Version Numbers String
- Find the Duplicate Number Array, Binary Search
- Rotate Array Array, Math
Hard (14)
- Longest Chunked Palindrome Decomposition String, Dynamic Programming, Greedy
- Minimum Changes to Make K Semi-palindromes String, Dynamic Programming
- Closest Subsequence Sum Array, Bitmask, Sorting
- Maximum Score of a Good Subarray Array, Stack, Monotonic Stack
- Kth Smallest Product of Two Sorted Arrays Array, Binary Search
- Subsequence With the Minimum Score String, Binary Search
- Last Substring in Lexicographical Order String
- Count Subarrays With Fixed Bounds Array, Sliding Window, Queue
- Maximum Number of Robots Within Budget Array, Sliding Window, Queue
- Minimum Window Subsequence String, Dynamic Programming, Sliding Window
- Minimum Number of Moves to Make Palindrome String, Greedy
- Find K-th Smallest Pair Distance Array, Binary Search, Sorting
- Subarrays with K Different Integers Array, Hash Table, Sliding Window
- Trapping Rain Water Array, Dynamic Programming, Stack
Companies that ask two pointers problems
- Amazon 134 problems on two pointers
- Google 81 problems on two pointers
- Microsoft 40 problems on two pointers
- Adobe 32 problems on two pointers
- Meta 23 problems on two pointers
- TCS 15 problems on two pointers
- Infosys 11 problems on two pointers
- Bloomberg 9 problems on two pointers
- Flipkart 9 problems on two pointers
- Wipro 6 problems on two pointers
- Zoho 6 problems on two pointers
- Apple 5 problems on two pointers
Next topic: Sliding Window