AI engineering glossary · Data structures algorithms

What is two pointers?

Two pointers: A technique that moves two indices through a sequence, often from both ends or at different speeds, to solve a problem in one pass instead of nested loops. It turns many O(n^2) checks into O(n).

Level: intermediateTopic: Data structures algorithms

Two pointers: an example

To find two numbers summing to 10 in the sorted list [1, 3, 4, 6, 9], move a left pointer up when the sum is too small and a right pointer down when too large.

Related terms

More Data structures algorithms terms

Where you learn it

Two pointers is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.