Arrays & Two Pointers

Data Structures & Algorithms · Array Techniques · Basics · 14 min

Walk from both ends of a list at once to solve problems in one pass.

Two friends stand at opposite ends of a row of lockers. On every step they look at their lockers, decide, and each takes a step toward the middle until they bump into each other.

The idea in everyday words

An array is a row of numbered lockers: locker 0, locker 1, locker 2… You can open any locker instantly if you know its number. Two pointers = two friends starting at opposite ends of the hallway, walking toward each other and comparing what they find.

Arrays in 30 seconds

  • Items sit side by side in memory, each with an index starting at 0.
  • Reading arr[5] is O(1) – instant.
  • Inserting in the middle is O(n) – everyone has to shift over.

The two-pointer trick

Put left at the start and right at the end. Compare, then move one (or both) inward. Stop when they meet.

Example: is "racecar" a palindrome?

  • r == r ✅ move in → a == a ✅ → c == c ✅ → pointers meet → yes!

Use it for:

  • Opposite ends (left at the start, right at the end, walking toward each other): palindromes, reversing, pair sums in a sorted array.
  • Same direction (a slow "write" pointer and a fast "read" pointer, both starting at the left): removing duplicates from a sorted array, moving zeros to the end. The fast pointer reads every item; the slow one marks where the next item you keep should go.

Step by step

  1. Setup (pair sum):

    Sorted nums = [1, 3, 4, 6, 8, 11, 15], target 14. left = 0 (value 1), right = 6 (value 15).

  2. 1 + 15 = 16:

    Too big. 15 is too big even with the smallest number, so it can't be in any pair: right = 5.

  3. 1 + 11 = 12:

    Too small. 1 is too small even with the biggest number left, so drop it: left = 1.

  4. 3 + 11 = 14:

    Found it: indexes 1 and 5. Every step threw one number away for good, so at most n steps: O(n).

  5. Same walk, new job:

    The challenge reverses a list. The pointers walk the same way, but at each step they SWAP arr[left] and arr[right], then both move inward until left >= right.

Code example

def is_palindrome(s):
    left, right = 0, len(s) - 1
    while left < right:
        if s[left] != s[right]:
            return False
        left += 1
        right -= 1
    return True

print(is_palindrome("racecar"), is_palindrome("hello"))

Complexity

  • Time: O(n)
  • Space: O(1)

Interview tips

  • Clarify: should spaces/capital letters be ignored?

  • Pair-sum in a sorted array: if sum too small move left++, too big move right--.

  • Mention edge cases: empty string, one character.

Challenge: Reverse a List In-Place

Reverse the list in place: change the list you were given (don't build a new one) by swapping from both ends with two pointers. Then return that same list.

No built-in reverse, reversed() or [::-1]. The tests check that the ORIGINAL list was changed.

Example: reverse_list([1, 2, 3, 4]) → [4, 3, 2, 1]

Solve it in the browser with tests, hints and an AI coach.

Practice problems

More in Array Techniques

Previous: B-Trees & Skip Lists: How Databases Index · Next: Fast & Slow Pointers