Coding Pattern Recognition (Two Pointers, Sliding Window) โ Revision Notes
Most coding-interview problems are variations of ~15 recurring patterns. Recognising the pattern from the problem's shape lets you jump to the right technique instead of reinventing it. Two of the highest-yield patterns are two pointers and sliding window.
High-yield patterns and their tells
| Pattern | Tell / when to use |
|---|---|
| Two pointers | Sorted array, pair/triplet with a target, in-place reversal |
| Sliding window | "Longest/shortest subarray/substring with condition" |
| Fast & slow pointers | Cycle detection, middle of a linked list |
| Binary search | Sorted input, or "minimise the max / search on answer" |
| BFS / DFS | Trees, graphs, grids, connected components |
| Dynamic programming | "Number of ways", "min/max cost", overlapping subproblems |
| Backtracking | Permutations, combinations, subsets, constraint puzzles |
Exam Tricks & Tips
- ๐ฏ "Contiguous subarray/substring + a constraint" โ sliding window (expand right, shrink left).
- ๐ฏ "Sorted + find a pair/triplet" โ two pointers from both ends.
- ๐ฏ "Sorted + search / minimise-the-max" โ binary search, even on the answer space.
- ๐ฏ Build a mental pattern-to-tell map from ~50 practice problems โ recognition gets fast.
- ๐ฏ State the pattern out loud when you spot it โ it shows experience.
- โ Common mistake: treating every array problem as brute force instead of spotting the window/two-pointer shape โ O(nยฒ) instead of O(n).
Expected pattern: Underlies nearly all DSA rounds; recognising the pattern is often the whole battle.
Quick recap: Learn ~15 patterns and their tells; match the problem's shape to a pattern; two-pointer and sliding-window cover a huge share.
Coding Pattern Recognition (Two Pointers, Sliding Window) โ Flashcards (Interview Prep)
Cover the answer, recall, then check. 11 cards on coding patterns.
Q1. Roughly how many patterns cover most problems?
A1. About 15 recurring patterns.
Q2. Tell for the sliding-window pattern?
A2. "Longest/shortest contiguous subarray or substring with a condition."
Q3. How does sliding window work mechanically?
A3. Expand the window's right edge, shrink from the left when the condition breaks.
Q4. Tell for the two-pointer pattern?
A4. A sorted array where you need a pair/triplet summing to a target, or in-place reversal.
Q5. Pattern for cycle detection in a linked list?
A5. Fast & slow pointers (Floyd's).
Q6. When to reach for binary search?
A6. Sorted input, or "minimise the max / search on the answer" problems.
Q7. Tell for dynamic programming?
A7. "Number of ways" or "min/max cost" with overlapping subproblems.
Q8. Tell for backtracking?
A8. Permutations, combinations, subsets, or constraint puzzles.
Q9. Pattern for trees, graphs, and grids?
A9. BFS or DFS.
Q10. How do you get fast at recognition?
A10. Build a pattern-to-tell map by solving ~50 practice problems.
Q11. Common mistake in pattern recognition?
A11. Brute-forcing array problems (O(nยฒ)) instead of spotting a window/two-pointer shape (O(n)).
Coding Pattern Recognition (Two Pointers, Sliding Window)
Coding interviews reuse a small number of patterns dressed up in different stories. Candidates who recognise the pattern behind a problem solve it in minutes; those who don't reinvent it from scratch under pressure. Two of the most common and highest-yield patterns are two pointers and sliding window โ learn to spot their signals and you'll shortcut a huge share of array and string problems.
What this covers / why it matters: how to recognise and apply two pointers and sliding window, with the signals that trigger each. Pattern recognition is what turns a hard-looking problem into a familiar one.
The approach
Beginner โ two pointers
Use two indices moving through the data, often from opposite ends or at different speeds. Signals: a sorted array, "pair/triplet that sums to X", "reverse in place", "remove duplicates in place", "is it a palindrome". Two pointers usually turn an O(n^2) nested loop into O(n) with O(1) space.
- Opposite ends: move the left/right pointer inward based on a comparison (e.g. sorted two-sum: if sum too small, move left up; too big, move right down).
- Fast/slow: detect a cycle, or find the middle of a linked list.
Intermediate โ sliding window
Maintain a moving window (a contiguous subarray/substring) and expand/shrink it. Signals: "contiguous subarray/substring", "longest/shortest ... satisfying a condition", "at most k distinct", "maximum sum of size k". You expand the right edge to include more, and shrink the left edge when the window breaks the condition โ each element enters and leaves at most once, giving O(n).
Advanced โ know your window flavour
- Fixed-size window: compute a running sum/count over exactly k elements, slide by adding the new and removing the old.
- Variable-size window: grow until the condition breaks, then shrink from the left until valid again, tracking the best window seen.
Recognising which flavour a problem needs โ and combining the window with a hash map for "distinct count" conditions โ covers most substring problems.
Worked example
"Longest substring without repeating characters."
Pattern signal: "longest", "substring" (contiguous), condition on the window โ variable sliding window + a hash set for seen characters.
"I'll grow a window with a right pointer, adding each character to a set. When I hit a character already in the set, I shrink from the left โ removing characters until the duplicate is gone โ then continue. The answer is the largest window size seen."
For "abcabcbb": window grows to "abc" (size 3), hits the second 'a', shrinks left past the first 'a', grows again... the max stays 3. Each character is added and removed at most once โ O(n) time, O(min(n, charset)) space. Naming the pattern immediately gave the O(n) solution instead of an O(n^2) substring scan.
What interviewers look for
- Pattern recognition โ you name two pointers / sliding window from the signals.
- Correct pointer/window movement and why each moves.
- The O(n) insight โ each element processed a constant number of times.
- Adaptation โ you fit the pattern to the specific condition.
Do's and don'ts
- Do scan the problem for signal words (sorted, pair, contiguous, longest/shortest, at most k).
- Do state why each pointer moves the way it does.
- Don't brute-force subarrays/pairs when a pointer/window applies.
- Don't memorise code blindly โ understand why the window is O(n).
- Mnemonic: "Sorted pairs โ two pointers; contiguous best โ sliding window."
Not recognising the pattern and defaulting to nested loops over all pairs or all substrings (O(n^2) or worse). The signal words โ "sorted", "pair sums to", "contiguous", "longest/shortest with a condition" โ are the tell. Train yourself to pattern-match before coding.
- โ- Two pointers: sorted arrays, pair/triplet sums, in-place reversal/dedupe, palindromes โ O(n), O(1).
- โ- Sliding window: contiguous subarray/substring with a longest/shortest/at-most-k condition โ O(n).
- โ- Fixed window slides by add-new/remove-old; variable window grows then shrinks on condition break.
- โ- Combine a window with a hash map/set for distinct-count conditions.
- โMost array/string problems are a pattern in disguise. Learn the signal words for two pointers and sliding window, and you'll convert O(n^2) brute forces into clean O(n) solutions on sight.
Coding Pattern Recognition (Two Pointers, Sliding Window) โ Worked Example
Worked Example
Problem/Question: "Find the length of the longest substring without repeating characters." โ Recognise and apply the right pattern.
Solution/Model answer:
Pattern recognition: "This asks for an optimal contiguous substring under a constraint (no repeats). 'Contiguous + optimise' signals the sliding window pattern."
Applying the sliding window:
- Keep a window from left to right and a set/map of characters currently in the window.
- Expand the right edge one char at a time. If the new char is already in the window, shrink from the left (removing chars) until the duplicate is gone.
- Track the maximum window size seen.
Complexity: each character enters and leaves the window at most once, giving O(n) time, O(min(n, charset)) space โ far better than the O(n^3) brute force of checking all substrings.
Related cue: a sorted array + "find a pair/triplet" points to the two-pointers pattern (converge from both ends).
Answer/Takeaway: Learn to map problem cues to patterns: "contiguous subarray/substring + optimise" to a sliding window; "sorted array + pair/triplet" to two pointers. Recognising the pattern collapses a hard problem into a known O(n) template.
- โ- "Contiguous + longest/shortest/optimal" is the classic sliding-window signal.
- โ- "Sorted input + find pair summing to X" points to two pointers converging from the ends.
- โ- Both patterns turn O(n^2)/O(n^3) brute force into O(n) by avoiding recomputation.