DSA Crash Course
Learn how to recognize the pattern, explain why it works, and rebuild the template under interview pressure.
Which Pattern Should I Use?
Start with the question your algorithm must answer. These choices cover the guides below, not every algorithm pattern.
| Question | Starting choice | Important boundary |
|---|---|---|
| Which states are reachable, or belong to the same component? | BFS or DFS | Track the complete search state; the same position with a different budget can be a different state. |
| What is the fewest number of equal-cost moves? | BFS | First discovery fixes distance only for equal-cost edges. Varying costs need a different shortest-path algorithm. |
| Do repeated merges connect these items? | Union Find | Tracks connectivity, not paths. The standard template does not support deleting connections. |
| Which candidate should I process next as candidates change? | Heap | Exposes the highest-priority item, not a fully sorted sequence. Choose the comparator to match the goal. |
| Should a parent be processed before or after its children? | Tree Traversal | Let dependencies choose the order: pass context downward or collect child results upward. |
| Does a parent's choice restrict what its children can choose? | Tree DP | Return results for each relevant condition; one unconditional subtree maximum can lose a valid combination. |
| Can I maintain a contiguous valid range by moving boundaries forward? | Sliding Window | Prove repair is monotone. Signed sums can invalidate a positive-sum window argument. |
| Does a new value resolve waiting greater/smaller boundaries? | Monotonic Stack | Define what a pop proves and how equal values are handled. Expiration may require a deque. |
| Can a comparison safely eliminate a family of pairs? | Two Pointers | Sorted pairs, container sides, read/write positions, and slow/fast links need different proofs. |
| Can checking the middle rule out a whole half? | Binary Search | Use sorted values, or show that possible answers change once from failing to working. |
| Do different choices reach the same remaining subproblem? | Dynamic Programming | State must retain every condition affecting future choices; define unreachable states explicitly. |
| Must I enumerate constrained choices or arrangements? | Backtracking | Restore branch-local state, snapshot outputs, and prune only impossible completions. |
| Am I combining ranges, scheduling jobs, or counting overlaps? | Intervals | Define touching endpoints. Coverage, compatibility, and simultaneous demand are different goals. |
- 01
Graph Traversal: BFS and DFS
Choose a frontier, define a visited state, and distinguish reachability from shortest paths.
16 linked practice problems
- 02
Union Find: Connected Components
Keep a reusable disjoint-set template and recognize when incremental connectivity needs it.
4 linked practice problems
- 03
Heaps and Priority Queues
Maintain the next best candidate without repeatedly sorting every candidate.
8 linked practice problems
- 04
Tree Traversal: Order and State
Choose when a node is processed and what information travels between parent and child.
10 linked practice problems
- 05
Tree DP: Subtree Return Values
Separate a child's return contract from the answer computed across the whole tree.
4 linked practice problems
- 06
Sliding Window: Maintain a Contiguous Range
Define window validity, update state incrementally, and prove when a boundary can move only forward.
3 linked practice problems
- 07
Monotonic Stack: Resolve Waiting Candidates
Keep unresolved indices in order, identify what a pop proves, and account for equal values.
4 linked practice problems
- 08
Two Pointers: Eliminate Candidates Safely
Choose pointer roles and justify why each move discards no better answer.
12 linked practice problems
- 09
Binary Search: Search a Sorted Array
Compare the middle, discard one half, and learn how to find a value or its first matching position.
6 linked practice problems
- 10
Dynamic Programming: Define State Before Storage
Derive state, transitions, and dependency order before compressing the table.
13 linked practice problems
- 11
Backtracking: Choose, Explore, and Restore
Explore a decision tree with path-local state, justified pruning, and independent output snapshots.
8 linked practice problems
- 12
Intervals: Merge, Schedule, and Sweep
Choose endpoint semantics and distinguish connected ranges, compatible selections, and simultaneous activity.
6 linked practice problems
Before the Interview
- Explain why the frontier or returned state is sufficient.
- Trace a counterexample to your first plausible shortcut.
- State time, memory, and JavaScript recursion limits separately.
- Rebuild a template, then solve one problem without looking at its solution.