Comparison Mode
Side-by-side: use case, requirements, complexity, strengths, weaknesses, example problems, and a clear “choose this when…”.
BFS vs DFSDijkstra vs Bellman-FordKruskal vs PrimMerge Sort vs Quick SortHeap vs Priority QueueHash Map vs Tree MapBFS vs DijkstraSliding Window vs Two PointersPrefix Sum vs Segment TreeGreedy vs Dynamic ProgrammingMemoization vs TabulationTarjan vs KosarajuSegment Tree vs Fenwick TreeArray vs Linked ListStack vs QueueQuick Sort vs Heap SortKMP vs Rabin-KarpUnion-Find vs DFSTrie vs Hash MapAVL Tree vs Red-Black Tree
Sliding WindowSliding Window | Two PointersTwo Pointers | |
|---|---|---|
| Use case | Best contiguous subarray/substring satisfying a constraint on its contents. | Pairs or partitions in ordered data, in-place compaction, palindromes, merging. |
| Requirements | Window validity must be monotonic under extend/shrink; usually a running sum or frequency map. | Sorted input or some ordering rule that tells you which pointer to move. |
| Time complexity | O(n); each index enters and leaves the window once. | O(n) after any required O(n log n) sort. |
| Space complexity | O(1) or O(k) for the frequency map of the window. | O(1). |
| Strengths | Turns O(n^2) subarray enumeration into a single pass; natural for "at most k" wording. | Zero extra memory; works from both ends; also expresses fast/slow list tricks. |
| Weaknesses | Fails when validity is not monotonic (e.g. sum with negative numbers); "exactly k" needs two windows or a prefix map. | Needs a proof that skipped pairs are safe; usually requires sorting first, which destroys indices. |
| Example problems | Longest substring without repeating characters, minimum window substring, max consecutive ones III. | Two sum II, three sum, container with most water, remove duplicates from sorted array. |
| Choose this when | Choose a sliding window when the answer is a contiguous range and growing/shrinking it changes validity monotonically. | Choose two pointers when the answer is a pair or a rearrangement of a sorted (or orderable) sequence, or an in-place rewrite. |