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
StackStack/Queue | QueueStack/Queue | |
|---|---|---|
| Use case | LIFO: nesting, undo, recursion simulation, DFS, expression evaluation. | FIFO: BFS, level order, scheduling, buffering, sliding windows (deque). |
| Requirements | A dynamic array or singly linked list with a top pointer. | A circular buffer, a doubly linked list, or two stacks. |
| Time complexity | Push, pop, peek O(1). | Enqueue, dequeue, front O(1). |
| Space complexity | O(n). | O(n). |
| Strengths | Trivial array implementation; models nested structure exactly. | Preserves arrival order; exactly what BFS needs for distance layers. |
| Weaknesses | Only the top is reachable; no fairness/order preservation. | Naive array shift is O(n); needs a circular buffer or deque for real O(1). |
| Example problems | Valid parentheses, min stack, daily temperatures, largest rectangle in histogram. | Binary tree level order, rotting oranges, word ladder, sliding window maximum (deque). |
| Choose this when | Choose a stack when the most recently added item is the one you must handle next: brackets, nesting, DFS, monotonic stacks. | Choose a queue when items must be processed in the order they arrived: BFS, level order, producers and consumers. |