Skip to content
BENIBIX Learn

Two Pointers

Development🧮 Algorithms & Data StructuresAlgorithm Design Techniques

Also known as Two-Pointer Technique, Two Pointer Method

Walking a list with two markers at once. Often one starts at each end.

Think of it like

Think of it like two friends on a bench. They start at the ends and walk toward each other.

Example

Emma checks if the word level reads the same both ways. One marker starts at each end, and they meet in the middle with every letter matching.

Why it matters

It is one of the most common tricks for fast list code. Job interviews ask about it a lot.

Definition

The two pointers technique walks a list with two markers at once. Often one starts at each end and they move toward each other. On a sorted list, this can find two numbers that add up to a target in one pass. It turns many O(n²) loops over every pair into a single O(n) walk. It also checks whether a word reads the same backwards.

Common mistake

Using it on an unsorted list for a sum. The moves only make sense when the list is in order.

Key takeaways

  1. 1
    Two markers. Often at both ends.
  2. 2
    Move toward each other. Based on what they see.
  3. 3
    One pass. O(n) instead of O(n²).