5 Coding Interview Patterns Every Candidate Should Know
February 15, 2025 | CheatCodeAI Team
Introduction
Most coding interview questions are variations of a handful of core patterns. Once you recognize the pattern behind a problem, the solution often writes itself. Here are five patterns that show up again and again.
1. Sliding Window
Use this when a problem asks you to find a subarray or substring that meets certain criteria — like the longest substring without repeating characters or the maximum sum of a subarray of size k.
The idea is simple: maintain a window defined by two pointers and expand or shrink it as you iterate through the input. This turns a naive O(n²) approach into O(n).
When to use it: Any problem involving contiguous sequences with constraints on length, sum, or uniqueness.
2. Two Pointers
Two pointers work well on sorted arrays or when you need to compare elements from different positions. Classic examples include finding pairs that sum to a target or removing duplicates in place.
Start one pointer at the beginning and one at the end, then move them toward each other based on the comparison result. This technique avoids nested loops and keeps things linear.
When to use it: Sorted arrays, pair-finding problems, or in-place array modifications.
3. BFS and DFS on Graphs and Trees
Graph traversal is everywhere in interviews. Breadth-first search is your go-to for shortest-path problems in unweighted graphs. Depth-first search works well for exploring all paths, detecting cycles, and solving tree problems.
The key is recognizing that many problems are graph problems in disguise. A grid is a graph. A word ladder is a graph. Dependencies between tasks form a graph.
When to use it: Trees, grids, shortest path, connected components, topological sorting.
4. Dynamic Programming
DP problems ask you to make a series of decisions where each choice depends on previous ones. The trick is identifying the subproblem structure and figuring out how to store intermediate results.
Start with a recursive solution, add memoization, then optionally convert to a bottom-up table. Do not jump straight to the table — the recursive approach makes the logic clearer.
When to use it: Optimization problems (min/max), counting problems, or anything where brute force involves repeated subproblems.
5. Binary Search
Binary search goes beyond searching a sorted array. Any time you can define a condition that splits the search space in half, binary search applies. This includes problems like finding the minimum capacity to ship packages or the kth smallest element.
When to use it: Sorted data, monotonic conditions, or when the answer lies in a searchable range.
Putting It Together
The hardest part of a coding interview is not writing the code — it is recognizing which pattern fits. Practice categorizing problems by pattern rather than memorizing individual solutions. When you see a new problem, ask yourself: does this involve a window? A sorted structure? Repeated subproblems?
With CheatCodeAI, you get real-time pattern recognition during your interview. The app detects the problem on screen and surfaces both brute-force and optimal approaches, so you can focus on communicating your thought process instead of panicking over the algorithm.
Want a safety net for your next coding interview? Try CheatCodeAI free.