Reddit Experience · 2026 Q1

Two pointers and sliding window are the same idea and it took me too long to see that

187 upvotes 16 replies

Interview Experience

I used to treat two pointers and sliding window as completely different things. Like two separate topics to memorize separately. Took me embarrassingly long to realize they're basically the same idea.

Full Details

I used to treat two pointers and sliding window as completely different things. Like two separate topics to memorize separately. Took me embarrassingly long to realize they're basically the same idea. Both are just ways to avoid redoing work you already did. You move pointers instead of restarting from scratch every time because restarting is O(n²) and moving is O(n). What actually made it click was asking one question before every problem. Is there a window or subarray I'm tracking here, if yes sliding window. Is there a pair or two ends of something I'm comparing, if yes two pointers. Most of the time it's obvious once you ask it. Where I kept going wrong early on was trying to figure out the pointer logic before understanding what I was actually tracking. I'd jump straight to "okay left pointer here, right pointer there" without knowing what left and right even represented in the problem. Never worked. Now I always write down in plain words what my window or my two positions mean before touching code. Takes like 30 seconds and it stops so many wrong approaches before they waste 20 minutes of your time. Also variable size vs fixed size windows are genuinely different to think about even if the code looks similar. Variable size usually has a condition that shrinks the window from the left. Fixed size just slides the whole thing. Worth keeping that distinction in your head. Once these two clicked a huge chunk of array and string problems just became variations of the same thing. What topic took you the longest to actually feel comfortable with?
Free preview — Unlock all questions →

Topics

Arrays Strings Two Pointers