Loading…
Loading…
Charting your lesson…
Opening your lesson…
Coding Pattern Recognition (Two Pointers, Sliding Window)
Lesson 4 of 4
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
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.
Step 2/ 61 min
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.
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).
Step 3/ 61 min
"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.
Step 4/ 61 min
Step 5/ 61 min
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.
Step 6/ 6· Worked examples1 min
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:
Recall
Keep this
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.
| 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 |
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.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.