AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Two Pointers coding problems

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.

New to two pointers? Read the Two Pointers Technique tutorial first.

165 problems · 61 easy · 90 medium · 14 hard · judged by hidden tests in 13 languages

165problems

61 easy

90 medium

14 hard

Sign in to tick off what you solve.

Study plan: 14 problems · about 8 h 10 min

Start the study plan

Next up: Reverse String

How it works

Two Pointers, step by step

Pair with a target sum in a sorted array, with two pointers

nums = [1, 2, 4, 6, 8, 11, 15], target = 14

nums1021426384115156leftrighttarget = 14pairs still possible: 21 of 21

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.

0:01 / 0:26Step 1 of 7
Transcript · 7 steps

Study plan

Learn two pointers in 8 days

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.

  1. Day 1

    about 1 h 15 min

    Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

  2. Day 2

    about 55 min

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

  3. Day 3

    about 1 h 10 min

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

  4. Day 4

    about 1 h 10 min

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

  5. Day 5

    about 1 h 10 min

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

  6. Day 6

    about 50 min

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

  7. Day 7

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

  8. Day 8

    about 50 min

    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 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

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 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.

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 (61)

Medium (90)

Hard (14)

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