
Sliding window or two pointers? Ask the invariant.
Most posts explain what each one is. This one explains the one question that tells you which to use — and why senior engineers still get it wrong under pressure.
The real question isn't "is this sliding window or two pointers?" -- it's "what invariant am I maintaining?" Once you see the invariant, the pattern picks itself.
Sliding window and two pointers are both techniques for walking an array with two indices — you just advance them differently. Sliding window keeps a bounded range; two pointers hunt toward each other or toward a target. Interview rounds test whether you pick the right one for the invariant you need.
Dan Luu -- who has shipped real work at Google, Microsoft, Twitter, and more -- openly admits he can't pass algorithms interviews. He's a highly compensated engineer whose production code runs at scale, and the pattern-matching gauntlet still beats him under time pressure.
So when you're staring at an array problem and can't tell whether it's sliding window or two pointers, it's not because you're missing something fundamental. It's because pattern-matching has a ceiling, and you've hit it.
There's a better way to think about these two patterns. Stop trying to classify the problem and start asking one question about it.
The One Question
When you see an array problem involving "find the longest," "count the pairs," or "find the smallest window," ask:
What am I maintaining as I move through the array?
Every time you move a pointer -- left, right, both -- something about your current position has to stay true. That "something" is the invariant. The invariant tells you which pattern you're using.
- If the invariant is about a window being valid (sum at most k, all distinct characters, no more than two zeros) -- .
- If the invariant is about a structure holding (array is sorted, sum is exactly k, pointers haven't crossed) -- .
Sliding Window: Maintaining a Window Constraint
The problem: given an array of integers and a target k, find the length of the longest subarray whose sum is at most k.
You have left and right. Expand right to grow the window. If the sum exceeds k, shrink from left until it's valid again. The answer is the max window size you ever saw.
What you're maintaining: the window is valid (sum at most k) at every step. The moment it would become invalid, you shrink until it's valid again. That constraint is the invariant. That's what makes it sliding window.
At every moment, you're in one of two states: either the window is valid (keep expanding) or you're shrinking it back to valid. The window is never invalid at a stable state. That's the invariant.
Two Pointers: Maintaining a Structural Invariant
Different problem: given a sorted array and a target sum, find two numbers that add to the target.
Put one pointer at the start, one at the end. Compute the sum.
- Too high? Move
rightdown. - Too low? Move
leftup. - Exact? Done.
The invariant here is "left has not crossed right, and the array between them still contains a possible answer." You're narrowing the search space without ever losing a valid pair. The sortedness of the array is what makes this guarantee possible.
A subtler example: move all zeros to the end while keeping the relative order of non-zero elements. No sortedness. No sum target.
But the invariant is hiding in plain sight. One pointer (write) tracks where the next non-zero goes. Another (read) scans forward. At every step: everything to the left of write is a non-zero element in its final position. That's the invariant. The sortedness isn't in the data -- it's in the partition you're maintaining.
If you tried to pattern-match "move zeros = sliding window or two pointers?" you'd be stuck for minutes. If you asked "what must stay true as I scan?" the answer walks you directly to the solution.
When Interviewers Ask This
| Problem type | Invariant | Pattern |
|---|---|---|
| Longest substring with at most K distinct chars (LC 340) | Window has at most k distinct chars | Sliding window |
| Two Sum on sorted array (LC 167) | Pointers haven't crossed, answer is between them | Two pointers |
| Minimum window substring (LC 76) | Window contains all target chars | Sliding window |
| Container with most water (LC 11) | Wider container is always still possible inward | Two pointers |
| Max consecutive ones with K flips (LC 1004) | Window has at most k zeros | Sliding window |
| Move zeroes (LC 283) | Everything left of write pointer is finalized | Two pointers |
The Decision, Mechanically
Before the diagram, here's the same answer before and after internalizing the invariant question. Both candidates eventually solved the problem. One got the offer.
How would you find the longest substring with at most k distinct characters?
“I think this is a sliding window problem. I'd use a hash map to track characters and slide the window when it gets too big.”
Names the pattern but stops there. The interviewer can't tell if you understand WHY sliding window.
“The invariant is that the window has at most k distinct characters. So I'll grow the right pointer until the invariant breaks, then shrink from the left until it holds again. The hash map tracks character counts so I know when a character drops to zero and the distinct count goes down.”
Names the invariant FIRST, then derives the pattern from it. The interviewer knows you'd recognize this structure on a problem you've never seen.
The question to ask yourself in the first 30 seconds:
Don't start with "which pattern is this?" Start with the invariant. The invariant is objective -- you can read the problem statement and identify it in a sentence. The pattern falls out of the invariant.
Why This Matters Beyond the Interview
Hello Interview makes the point directly: interviews are not about getting to a single right answer -- the interviewer is assessing your ability to navigate complexity, reason about trade-offs, and communicate clearly.
The invariant question does exactly that. When you walk the interviewer through "here's what I need to maintain as I move through this array" -- whether that's a valid window or an unexplored sorted region -- you're demonstrating the skill they're grading for. You're reasoning from the problem's structure to the algorithm's structure.
And if you genuinely can't tell whether a problem is sliding window or two pointers, the honest answer is: neither, yet. You haven't pinned down the invariant. Go back and re-read the problem statement.
The Bottom Line
- Sliding window = you're maintaining that a window is valid.
- Two pointers = you're maintaining that a structure is preserved.
When Dan Luu can't pass an algorithms interview, it's not because he doesn't know both patterns. It's because pattern-matching under time pressure has a ceiling. The invariant question is the ladder past that ceiling.
Next time you see an array problem, try this: before you pick a pattern, write one sentence that starts with "At every step, I need to keep true that..." The sentence you write is the invariant. The pattern is whatever maintains it.
Sources
- Dan Luu -- "Algorithms interviews" -- the opening hook on senior engineers failing algo interviews
- Hello Interview -- System Design Learning -- "not about getting to a single right answer" framing
- LeetCode Sliding Window Tag -- full problem set
- LeetCode Two Pointers Tag -- full problem set
- NeetCode Two Pointers Roadmap -- curated progression
Fin and Coco are StrongYes editorial personas from the Council of Ternary Vertices — a trinary-star animal civilization that studies Earth's coding-interview process. Anecdotes map animal-universe experience to human interview mechanics; they are NEVER human-career claims. External citations link to public primary sources.
StrongYes editorial guide synthesized from real engineer commentary on algorithm interview pressure (Dan Luu), trade-off framing from Hello Interview, and the StrongYes coaching library.
Reviewed by Leo Kwan on Apr 12, 2026.
Practice Sliding window.
Reading builds recognition. Explaining builds recall. Run these problems with Fin or Coco.