Pattern Comparisons
Side-by-side breakdowns of the most commonly confused DSA patterns. Learn exactly when to reach for each one.
Know when to use which for array and string problems
Both traverse arrays linearly, but Two Pointers works best on sorted data while Sliding Window tracks contiguous subarrays with constraints.
Choose the right traversal for trees and graphs
BFS finds shortest paths and processes level-by-level. DFS goes deep first and is ideal for exhaustive search, backtracking, and path finding.
The critical difference in item reuse changes the recurrence
0/1 Knapsack uses each item at most once. Unbounded Knapsack allows unlimited reuse. The DP transition differs in one key way.
When a local choice is enough vs when you need the full picture
Greedy picks the locally optimal choice at each step. DP explores all subproblems when greedy fails. Knowing which to use saves hours in interviews.
Two approaches to interval and scheduling problems
Merge Intervals sorts and merges overlapping ranges. Line Sweep uses events (start/end points) for counting overlaps, scheduling, and more.
When DFS is sufficient vs when you need the undo step
DFS explores; backtracking explores and undoes. Use backtracking when enumerating all solutions (permutations, N-Queens) and plain DFS for path existence or component counting.
Both use hash maps but for different purposes
Hash Map Counting tracks frequencies. Two Sum Pattern finds complements. Know when to count vs when to find pairs that sum to target.
Shortest path vs dependency ordering
BFS finds shortest paths and level-order traversal. Topological sort finds valid orderings in DAGs. Kahn's algorithm uses BFS-style queue for ordering, not distances.
Running max vs cumulative precomputation
Kadane's finds max subarray sum in one pass. Prefix sum precomputes for range queries and subarray sum equals K. Know when each is appropriate.
When prefix-based lookup matters
Trie excels at prefix search, autocomplete, and shared structure. Hash map for exact lookups. Understand the space/time tradeoffs for string problems.