DSA Patterns That Actually
Matter in Interviews
Stop memorizing problems. Start recognizing patterns.
You don't need to memorize 500 LeetCode problems. You need to recognize 12 patterns. Here are the 5 that unlock the vast majority of interview questions — with the intuition behind each one.
Most people approach interview prep the wrong way: they grind problems one by one, hoping pattern recognition emerges from sheer volume. It rarely does — because they're memorizing solutions, not building a mental model for why a solution works.
A pattern is a reusable abstract strategy. When you encounter a new problem, the question isn't "have I seen this before?" — it's "what shape does this problem have?" Once you can identify the shape, the approach follows directly. This article is about training that identification reflex.
"Don't practice until you get it right. Practice until you can't get it wrong."
The goal is not to solve problems — it's to instantly see which category a problem belongs to within 60 seconds of reading it.
view_column Sliding Window
linear · contiguous · O(n)
The Intuition
Imagine reading a book through a fixed-size rectangular cutout that you slide from left to right. At each position, you can see exactly k words. Instead of re-reading from scratch every time you move the cutout, you just drop the leftmost word and add the new rightmost one. That's the entire idea.
The insight that makes this powerful: when you move the window by one step, 99% of the information stays the same. Only one element enters and one exits. A brute-force approach recomputes everything from scratch at each position — O(n·k). The window maintains a running result — O(n).
Array · window size = 3
Drop left element, add right — result updates incrementally
Fixed vs Variable Window
Fixed window: the size is given (k). Variable window: you expand the right boundary until a condition is violated, then shrink from the left. Variable windows solve problems like "longest substring without repeating characters" — the window grows greedily and shrinks as needed.
compare_arrows Two Pointers
sorted arrays · pairs · O(n)
The Intuition
Two pointers is what you naturally do when you search for two numbers in a sorted list that add up to a target: you put one finger at the beginning, one at the end, and move them toward each other. If the sum is too big, move the right pointer left. If too small, move the left pointer right.
The key insight: sorting gives structure that lets you eliminate large portions of the search space with a single comparison. Without sorting, you'd have to check every pair — O(n²). With sorting and two pointers, each step eliminates either everything to the left of the left pointer or everything to the right of the right pointer — O(n).
Same-direction vs Opposite-direction
Two pointers don't always start at opposite ends. The "fast and slow" variant (two pointers moving in the same direction at different speeds) detects cycles in linked lists and finds the middle node in a single pass. The mental model shifts: instead of converging, one pointer is always ahead of the other by some constant or multiple.
table_chart Hashing
frequency · lookup · O(1) access
The Intuition
A hash map is a memory trade-off tool: you trade space for time. Instead of scanning the array again to answer "have I seen X before?" — you record the answer the first time you see X. Lookups that would cost O(n) in a loop become O(1).
The deeper insight: a hash map transforms a search problem into a lookup problem. And lookup is the fastest operation in computer science. Whenever you find yourself writing a nested loop to search for a complement, a pair, or a previous state — ask if a hash map can precompute that.
if x in array inside a loop — use a set or map instead.The Prefix Sum + Hash Pattern
A powerful combination: compute a running sum as you traverse, and store each sum in a hash map. To find a subarray with sum k, you check if current_sum - k exists in the map. This converts a cubic brute force into a linear solution — and the logic is the same hashing insight applied to accumulated state rather than individual elements.
stacks Stack & Monotonic Stack
order · "next greater" · O(n)
The Intuition — Regular Stack
A stack models problems where the most recently seen thing is the most relevant. Matching brackets is the canonical example: when you see a closing bracket, the bracket you need to match is the last unclosed opening bracket — not the first one you saw. Last In, First Out is the right semantics.
The signal: if you're about to write a problem where you need to "remember" something until a matching event occurs — think stack. Parsing expressions, matching parentheses, undo history — all stack-shaped problems.
The Intuition — Monotonic Stack
A monotonic stack is a stack with a discipline: you only push an element after popping all elements that violate the ordering invariant. This sounds abstract; the effect is concrete and powerful.
For Next Greater Element: traverse left to right. For each element, pop the stack while the top is smaller than the current element — those popped elements have found their "next greater". Push the current element. At the end, everything remaining in the stack has no next greater element.
The insight: the stack always holds "unresolved" elements — elements waiting for their answer. Each element is pushed and popped at most once, making the total work O(n) despite the nested-looking structure.
account_tree Recursion & Backtracking
decision tree · explore · prune
The Intuition — Recursion
Recursion is the art of trusting a smaller version of the problem. You define the answer for the base case, then assume your function already works for a smaller input and use that to build the answer for the current input. The hardest part isn't writing the recursion — it's trusting that it works without mentally simulating every frame.
The mental model: think about what one step buys you, and delegate the rest. To reverse a linked list: reverse everything after the first node (delegate), then attach the first node at the end (one step). The delegation is the recursive call.
Backtracking: Recursion with a Constraint
Backtracking is systematic trial and error on a decision tree. At each step, you make a choice, recurse, and if the current path can't lead to a solution — you undo the choice (backtrack) and try the next option. It's a depth-first traversal of the space of possibilities with pruning.
The power is in the pruning: instead of generating all combinations and filtering, you abort a branch the moment you know it can't succeed. For a Sudoku solver, you don't try all 9^81 combinations — you fill cells one by one and immediately backtrack when you violate a constraint.
psychologyHow to Use Patterns in an Interview
Pattern recognition is a skill with a specific training loop. When you solve a problem, don't just verify your answer — ask: what was the key insight that made this tractable? Label it. "This was a variable sliding window because the answer was a contiguous substring." "This was a monotonic stack because I needed the nearest smaller element."
Over time, you stop seeing problems and start seeing shapes. A problem involving contiguous ranges looks like a window. A problem involving ordered pairs in a sorted array looks like two pointers. A problem involving "have I seen this before?" looks like a hash map.
Beyond the Five
These five patterns cover a large fraction of medium-difficulty interview problems. The next tier — Binary Search on the answer, BFS/DFS on graphs, Dynamic Programming, Divide and Conquer — builds on the same fundamental habit: identifying structure before reaching for implementation. That habit is the real skill interviews are testing.
flagPattern Thinking, Not Problem Memorization
The developer who has solved 500 LeetCode problems by looking at solutions and repeating them will struggle when they see a slightly different problem. The developer who has internalized 12 patterns and trained the classification reflex will adapt to problems they've never seen.
The patterns in this article are not algorithms — they're lenses. Any single lens won't work on every problem. But five lenses, applied systematically in the first 60 seconds, will give you a starting point more often than not. And in an interview, a confident starting point is most of the battle.
If this helped you or saved you some time, consider supporting my work.
emoji_objects Key Takeaways
- Pattern recognition beats problem memorization — 12 patterns unlock the majority of interview problems you'll encounter.
- A sliding window transforms O(n·k) recomputation into O(n) by maintaining an incremental running state.
- Hash maps convert search into lookup — if you're scanning an array inside a loop, there's probably a hash-based O(1) solution.
- A monotonic stack holds 'unresolved' elements waiting for their answer — each element is pushed and popped at most once.
- Backtracking without pruning is brute force — the discipline of aborting branches early is what makes it viable.