Skip to content

Pattern Comparisons

Side-by-side breakdowns of the most commonly confused DSA patterns. Learn exactly when to reach for each one.

Two PointersvsSliding Window

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.

Read comparison
BFSvsDFS

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.

Read comparison
0/1 KnapsackvsUnbounded Knapsack

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.

Read comparison
GreedyvsDynamic Programming

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.

Read comparison
Merge IntervalsvsLine Sweep

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.

Read comparison
BacktrackingvsDFS

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.

Read comparison
Hash Map CountingvsTwo Sum Pattern

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.

Read comparison
BFSvsTopological Sort

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.

Read comparison
Kadane's AlgorithmvsPrefix Sum

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.

Read comparison
TrievsHash Map

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.

Read comparison