Two Pointers & Sliding Window
Cut O(n²) nested loops to O(n) by mastering the two-pointer and sliding window patterns.
Questions in this quiz
10 questions on Two Pointers & Sliding Window. Work through them here, then start the quiz to answer them and see your score.
1.When is the two-pointer technique most applicable?
- AWhen the input contains only negative numbers
- BWhen you need to count all subsequences
- CWhen the input is a tree or graph
- DWhen the input is sorted (or can be sorted) and you need to find pairs or a contiguous range satisfying a condition
2.What is the key insight that makes sliding window O(n) instead of O(n²)?
- AThe window size is always fixed
- BThe window never overlaps itself
- CYou only look at the first and last element
- DEach element enters the window at most once and leaves at most once — total operations are O(2n) = O(n)
3.What triggers shrinking the left pointer in a variable-size sliding window?
- AThe window sum becomes negative
- BA constraint is violated — e.g., duplicate characters found, sum exceeds target, or too many distinct elements
- CThe window becomes empty
- DThe right pointer reaches the end of the array
4.What does this code compute for nums = [2, 1, 5, 1, 3, 2], k = 3?
int k = 3, maxSum = 0, windowSum = 0; for (int i = 0; i < k; i++) windowSum += nums[i]; maxSum = windowSum; for (int i = k; i < nums.length; i++) { windowSum += nums[i] - nums[i - k]; maxSum = Math.max(maxSum, windowSum); } System.out.println(maxSum);- A9
- B7
- C5
- D6
5.What does this function return for s = "abcabcbb"?
public int lengthOfLongestSubstring(String s) { Map<Character, Integer> lastSeen = new HashMap<>(); int max = 0, left = 0; for (int right = 0; right < s.length(); right++) { char c = s.charAt(right); if (lastSeen.containsKey(c) && lastSeen.get(c) >= left) { left = lastSeen.get(c) + 1; } lastSeen.put(c, right); max = Math.max(max, right - left + 1); } return max; }- A2
- B4
- C7
- D3
6.The valid palindrome check (ignoring non-alphanumeric characters) — which approach is most space-efficient?
- AConvert to char array and sort
- BTwo pointers from both ends, skipping non-alphanumeric characters
- CUse a stack to reverse characters
- DReverse the cleaned string and compare
7.Given a sorted array, Two Sum II can be solved with two pointers. What is the space complexity vs HashMap approach?
- ATwo pointers: O(n), HashMap: O(1)
- BBoth O(n)
- CTwo pointers: O(1), HashMap: O(n)
- DBoth O(1)
8.The 3Sum problem asks for all unique triplets summing to zero. What is the optimal approach?
- ASort, fix one element (outer loop), then two-pointer on the remaining sorted subarray: O(n²)
- BThree nested loops: O(n³)
- CHashMap for all pairs, check complement: O(n²) time, O(n²) space
- DBinary search for each complement: O(n log n)
9.Container With Most Water: n vertical lines, maximize trapped water. What technique applies?
- ABinary search on the answer
- BDynamic programming
- CTwo pointers from both ends — always move the shorter line inward
- DSliding window of fixed size 2
10.Which problem type is a fixed-size sliding window best suited for?
- ATwo Sum in a sorted array
- BMaximum or minimum sum of any contiguous subarray of exactly size k
- CFinding a cycle in a linked list
- DLongest substring with unique characters