Two Pointers vs Sliding Window

Updated · By

A sliding window is a kind of two pointers. Use a sliding window when what lies between the indexes matters, such as a sum or character counts over a contiguous range. Use two pointers when only the values at the indexes matter, such as a pair in a sorted array, where the elements between them have not been ruled in or out. Both are usually O(n).

Two pointers and sliding window compared

Two pointersSliding window
What mattersThe two values under the pointersEverything between the two indexes
How the indexes moveFrom both ends inward, or one chasing the otherBoth forward, with the right leading
What you keepUsually nothing beyond the indexesA running summary: a sum, counts, a maximum
Needs sorted inputUsually, for a pair searchNo, but a sum window needs no negative numbers
Answer shapeA pair or triple, or an in-place rewriteA contiguous subarray or substring
TimeO(n), plus O(n log n) if you sort firstO(n), since each index only moves forward
SpaceO(1)O(1) to O(k), for the summary

A worked example where both apply

Take a sorted list of timestamps. The two functions below have almost the same loop, and the difference is what the question asks about. The first asks whether two events are exactly gap apart: only the two values matter. The second asks for the most events inside any span of gap seconds: now everything between the indexes counts.

def has_pair_with_gap(times, gap):
    """Two pointers: only the values at left and right matter."""
    left = 0
    for right in range(len(times)):
        while times[right] - times[left] > gap:
            left += 1
        if left < right and times[right] - times[left] == gap:
            return True
    return False

def most_events_in_span(times, gap):
    """Sliding window: everything between left and right counts."""
    best = left = 0
    for right in range(len(times)):
        while times[right] - times[left] > gap:
            left += 1                    # too wide: drop the oldest event
        best = max(best, right - left + 1)
    return best

Both are O(n) time and O(1) space, for the same reason: neither index ever goes back. In the first function, the left < right check is what stops an event pairing with itself when gap is zero.

Now change the data. If the list is not sorted, neither loop works as written: a pair search would use a hash set or sort first, and a window has nothing to shrink against. That is the quickest test of whether either technique applies at all.

Which to pick in an interview, and how to say why

  • Longest, shortest, or at most k over a contiguous range: a sliding window.
  • A pair or triple in sorted data: two pointers from the ends.
  • An in-place filter or a merge of two sorted lists: same-direction two pointers.
  • A sum over a range with negative numbers: neither. Use prefix sums with a hash map, because the window's rule no longer moves one way.
What a strong candidate might say

The answer is a contiguous range and I care about what's inside it, so this is a sliding window rather than a plain two-pointer search. I grow the right edge one step at a time, and while the span is too wide I move the left edge forward. Each index only moves forward, so it's O(n) time despite the nested loop, with O(1) space because the count is just right minus left plus one.

Try a mock interview free

3 interviews a month on the Free plan, up to 20 minutes each. No card needed.