Two Pointers Coding Problems: 113 Questions with Solutions

113 two pointers coding problems — 51 easy · 55 medium · 7 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 113
  • By difficulty: 51 easy · 55 medium · 7 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.

How two pointers works, step by step

nums1021426384115156leftright6 + 8 = 14, the targetpairs still possible: 1 of 21
Pair with a target sum in a sorted array, with two pointers. Example: nums = [1, 2, 4, 6, 8, 11, 15], target = 14
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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 113 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 99 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.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

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.

Next topic: Sliding Window

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 <= hi when a pair needs two distinct elements; use lo < 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

All two pointers problems

Easy (51)

Medium (55)

Hard (7)

Companies that ask two pointers problems

  • Amazon 89 problems on two pointers
  • Google 52 problems on two pointers
  • Adobe 24 problems on two pointers
  • Meta 15 problems on two pointers
  • TCS 15 problems on two pointers
  • Infosys 11 problems on two pointers
  • Microsoft 11 problems on two pointers
  • Flipkart 8 problems on two pointers
  • Wipro 6 problems on two pointers
  • Zoho 6 problems on two pointers
  • Bloomberg 4 problems on two pointers
  • Accenture 3 problems on two pointers

Next topic: Sliding Window