DSA Tracker

Blog

Patterns

Two Pointers vs Sliding Window: How to Tell Which One to Use

By Riya Kushwaha5 min read

Ask two questions before you touch the keyboard. Is the input sorted, or can you sort it without losing the answer? And is the answer a pair or triple of values, or is it a contiguous stretch of the array? Sorted plus pair points to two pointers. A contiguous stretch with a condition on its contents points to a sliding window.

The confusion is fair, because both patterns use two indexes and both turn an O(n^2) scan into O(n). The indexes just mean different things.

What the two indexes are doing

In two pointers, the indexes are two candidates. You hold a value at the left and a value at the right, look at their combined effect, and decide which one to discard. Sorted order is what makes the discard safe: if the sum is too large, the right value can never be part of an answer with anything to its left, so you drop it.

In a sliding window, the indexes are the two edges of one region. The window is a claim: "everything between left and right is my current candidate answer." You grow the right edge to take in more, and you move the left edge only when the window breaks a rule.

A quick test on real problems

Take Two Sum II, where the array is sorted and you need two numbers that hit a target. Two candidates, sorted input, discard one per step: two pointers.

Take Longest Substring Without Repeating Characters. Nothing is sorted, and the answer is a stretch of the string, not two separate characters. You extend the right edge, and when a repeat appears you shrink from the left until the window is valid again: sliding window.

Take 3Sum. You need three values, so it is not a window. Sort the array, fix one value, and run two pointers on the rest. This is the case where the patterns stack, and it is why people mix them up.

Take Minimum Size Subarray Sum with positive numbers. The answer is a contiguous stretch whose sum crosses a target, and adding an element only ever increases the sum. That monotonic behaviour is exactly what a window needs. Grow right until the sum is enough, then shrink left while it stays enough, and record the shortest length.

Where the window quietly breaks

A window only works when moving an edge changes the condition in a predictable direction. With positive numbers, a bigger window means a bigger sum, so you know which edge to move. Put negative numbers in the array and that promise disappears: growing the window can lower the sum, and the shrink step no longer knows when to stop. Subarray Sum Equals K with negatives is the standard example. It needs a running prefix sum stored in a hash map instead.

Two pointers has its own trap. Using it on unsorted data gives answers that look plausible on small cases and fail on larger ones. If the problem statement does not promise sorted input, check whether sorting first destroys the answer. Returning original indexes, as the classic Two Sum does, is the usual reason it does.

A habit that saves time

When you read a new problem, write one line before any code: "candidates" or "region." If you wrote candidates, you are choosing between values and you want sorted order. If you wrote region, you are tracking a stretch and you want a rule that tells you when the region is invalid. That one word decides the pattern, and it also tells you what to say out loud in an interview, which is half the marks.

Next step: pick two solved problems from your list, one you did with a window and one with two pointers, and write the "candidates or region" line beside each. If you cannot justify the line, redo that problem.

Practice what you just read

Keep reading