Two Pointers & Sliding Window
Part of the Java Roadmap
In short
Many problems have an obvious O(n²) answer: check every pair, or every subarray. Two pointers and sliding windows usually bring that down to a single O(n) pass, and interviewers love to see the jump.
Two pointers move through an array from both ends or at different speeds, and a sliding window tracks a moving range. Both patterns turn many O(n²) brute-force solutions into O(n).

Think of it like this
The train window view
Sitting in a train, your window shows a few houses at a time. As the train moves, one house slides in at the front and one slides out at the back. You never look at the whole village again, you just update what changed. That moving frame is a sliding window.
Watch it happen
Find two numbers that add up to 10
1/6The array is sorted. Start one pointer at each end.
The big ideas
What you need to know
Opposite ends
On sorted data, start at both ends and move the pointer that brings you closer to the target. Pair sums and palindromes use this.
Fast and slow
Two pointers moving at different speeds remove duplicates in place, or find the middle and cycles in linked lists.
Fixed window
For 'best sum of k consecutive items', add the new element and subtract the one leaving. O(n) instead of O(n × k).
Variable window
Grow the right edge, and shrink the left edge while the window breaks a rule. Classic for longest substring problems.
See it in code
Walk through the code
1import java.util.HashSet;2import java.util.Set;3 4public class TwoPointers {5 // Sorted array: find two numbers that add up to target6 static int[] pairWithSum(int[] nums, int target) {7 int left = 0, right = nums.length - 1;8 while (left < right) {9 int sum = nums[left] + nums[right];10 if (sum == target) return new int[] {left, right};11 if (sum < target) left++;12 else right--;13 }14 return new int[] {-1, -1};15 }16 17 // Variable window: longest substring with no repeated characters18 static int longestUnique(String s) {19 Set<Character> window = new HashSet<>();20 int left = 0, best = 0;21 for (int right = 0; right < s.length(); right++) {22 while (window.contains(s.charAt(right))) {23 window.remove(s.charAt(left));24 left++;25 }26 window.add(s.charAt(right));27 best = Math.max(best, right - left + 1);28 }29 return best;30 }31}1/4Pointers at both ends, moving towards each other until they meet.
pairWithSum(new int[] {1, 3, 4, 6, 8, 11}, 10) returns [2, 3]. longestUnique("abcabcbb") returns 3.
Save yourself the pain
Common mistakes
Oops
Using opposite-end pointers on an unsorted array.
Do this instead
The 'move the smaller side' logic only works on sorted data. Sort first, or use a HashMap instead.
Oops
Recomputing the whole window sum each time it slides.
Do this instead
Keep a running sum: add nums[right], subtract nums[right - k]. That is what makes it O(n).
Your mission
Pattern practice
Quick check
In a sliding window of size k over n numbers, how many additions and subtractions does a running sum need in total?
Go deeper
- 1Solve pair sum on a sorted array with two pointers.
- 2Find the longest substring without repeating characters with a sliding window.
- 3Practise 5 problems from each pattern and note the template you reuse.
Also part of these roadmaps
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.