Free course · 21 lessons · Beginner
DSA Patterns Made Simple
The coding-interview patterns that solve most problems, taught one at a time: how to spot each pattern, one short template to remember, a worked example in Python and JavaScript you can run, and real problems to practise on. Starts from Big O and a five-step method, so beginners can follow along.
1. Start Here: How to Solve Coding Problems
A repeatable five-step method for any coding problem, and Big O explained by counting steps, so every pattern that follows has a place to land.
- How to Solve Any Coding Problem in 5 Steps — A simple routine for the moment you read a new problem: understand it, try examples, write the slow version, find the pattern, then test. It works in interviews and in everyday work.
- Big O in Plain English — Big O tells you how much slower code gets as the input grows. Learn the six growth rates you will actually meet, how to read them from loops, and why a hash map often turns minutes into milliseconds.
2. Arrays and Strings: The Six Core Patterns
Most interview problems are about a list or a string. These six patterns (remember what you've seen, count, two pointers, sliding window, prefix sums and binary search) solve the majority of them.
- Hash Map: Remember What You've Seen — The most useful pattern of all: store what you've seen in a set or map so the next lookup is instant. It turns many O(n²) answers into O(n).
- Counting and Grouping — Count how often things appear, or group items that share a key. Anagrams, most-frequent elements and 'first unique character' all fall to a frequency map.
- Two Pointers — Walk inward from both ends, or move a slow and a fast index through the same array. Perfect for sorted arrays, palindromes and in-place clean-ups, with O(1) extra space.
- Sliding Window — For 'longest', 'shortest' or 'best' contiguous stretch problems, slide a window over the array, growing on the right and shrinking on the left, instead of checking every subarray.
- Prefix Sums — Precompute running totals once, then answer any 'sum of a range' in O(1). Combined with a hash map, it counts subarrays with a given sum, even with negative numbers.
- Binary Search (and Searching for an Answer) — Halve the search space each step to find a value in O(log n). The same idea finds the first position where a condition becomes true, which solves far more problems than plain lookups.
3. Linked Lists, Stacks and Intervals
Pointer tricks for linked lists, stacks for 'most recent first' problems, and the sort-then-sweep pattern for intervals and schedules.
- Fast and Slow Pointers — Move one pointer one step at a time and another two steps. They find the middle of a list, detect cycles and find the nth node from the end, all in one pass with no extra memory.
- Reversing and Merging Linked Lists — Three pointers (previous, current, next) reverse a list in place, and a dummy head node makes building or merging lists free of special cases.
- Stacks: Matching Pairs and the Next Greater Element — A stack remembers the most recent unfinished thing. Use it to match brackets, undo steps, evaluate expressions, and, as a 'monotonic stack', find the next warmer day for every day in one pass.
- Intervals: Sort, Then Sweep — Meetings, bookings and time ranges: sort by start time, then walk through once, merging or comparing neighbours. One idea solves merging, inserting and 'can I attend every meeting?'.
4. Trees and Graphs
Depth-first and breadth-first search on trees, grids and graphs, plus topological sort for 'what order do I do things in?'. Learn two traversals and you can answer most tree and graph questions.
- Tree DFS: Let Recursion Do the Work — Most tree problems are three lines of recursion: handle the empty tree, ask both children, combine their answers. Learn to trust the recursion and depth, inversion and BST checks become easy.
- Tree BFS: Level by Level — Process a tree one level at a time with a queue. Use it for level-order output, the right-side view, the minimum depth, and anything that says 'level' or 'closest'.
- Graphs and Grids: Flood Fill with DFS and BFS — A grid is a graph where each cell's neighbours are up, down, left and right. Count islands with a flood fill, and find shortest paths with BFS, remembering which cells you've already visited.
- Topological Sort: Ordering Tasks With Dependencies — Courses with prerequisites, build steps, package installs: when tasks depend on other tasks, repeatedly take the ones with no remaining prerequisites. If you get stuck, there's a cycle.
5. Heaps, Backtracking and Dynamic Programming
The last three patterns: heaps for 'top k', backtracking for 'all combinations', and dynamic programming for 'count the ways / best total'. Then a one-page guide to picking the right pattern.
- Heaps: Top K and Always-the-Smallest — A heap hands you the smallest (or largest) item instantly and keeps that true as items come and go. Use it for the k largest, the k most frequent, merging sorted lists and running medians.
- Backtracking: Try, Recurse, Undo — To list every subset, permutation or valid combination, build answers one choice at a time, recurse, then undo the choice and try the next. One template covers them all.
- Dynamic Programming: Remember Smaller Answers — When a problem's answer is built from answers to smaller versions of itself, and those smaller answers repeat, store them. Climbing stairs, house robber and coin change all use the same three questions.
- Which Pattern Should I Use? A One-Page Guide — A cheat sheet that maps the words in a problem statement and the input size to the right pattern, with the template to reach for. Use it on every new problem until it becomes automatic.
- Capstone: A Mock Coding Round — Three unseen problems in interview conditions. For each one, name the pattern, explain your plan out loud, code it, and test edge cases, then compare with the model answers and rubric.