Hourly DSABlog

Stack vs. Queue: LIFO, FIFO, and Interview Examples

By Hourly DSA · · 4 min read

A stack removes the most recently added item first. A queue removes the earliest added item first. These rules are called last in, first out (LIFO) and first in, first out (FIFO). Choosing between them means deciding which unfinished item your algorithm should handle next.

The distinction affects correctness. Replacing a queue with a stack can change a graph traversal's guarantees even if the program still visits every vertex.

Trace the same operations with both structures

Add A, then B, then C. Remove one item. Add D. Remove one more item.

With a stack, the first removal returns C. After adding D, the next removal returns D. A and B remain, with B on top.

With a queue, the first removal returns A. After adding D, the next removal returns B. C and D remain, with C at the front.

OperationStackQueue
Add an itemPush onto the topEnqueue at the back
Remove an itemPop from the topDequeue from the front
Inspect the next itemPeek at the topPeek at the front
Removal orderMost recent firstEarliest first

Peeking does not remove the item. Removing from an empty structure needs explicit handling; the particular API determines whether that produces an error or another result.

Why balanced brackets need a stack

In a correctly nested bracket sequence, a closing bracket must match the most recent unmatched opening bracket.

Consider ([]):

  1. Read ( and push it.
  2. Read [ and push it above (.
  3. Read ] and match it against the top, [.
  4. Read ) and match it against the remaining (.

The stack is empty at the end, so every opener has been matched.

Now consider ([)]. When ) arrives, the top is [, so the sequence fails immediately. Counting opening and closing brackets would miss the problem: the counts balance, but the nesting does not.

A correct check also rejects a closing bracket when the stack is empty and rejects leftover opening brackets after the scan. Those conditions cover inputs such as ) and ((.

The stack preserves exactly the history needed for the next decision: the most recent unmatched opener.

Why breadth-first search needs a queue

Imagine an unweighted graph with these directed edges:

A → B
A → C
B → D
C → E

Starting at A, enqueue B and C as you discover them. Process B next and add D behind C. C is still processed before D because it entered the queue earlier.

That order keeps vertices one edge from A ahead of vertices two edges away. It is the basis for BFS finding paths with the fewest edges in an unweighted graph.

Mark a vertex as discovered when you enqueue it. Otherwise, multiple parents can add the same vertex before it is processed.

A stack instead supports exploring a branch before returning to other pending branches. See BFS vs. DFS for how that changes traversal order and shortest-path reasoning.

Are stack and queue operations always O(1)?

The abstract rules define removal order, not implementation cost. A linked stack can push and pop at its head in O(1) time. A linked queue with both head and tail references can enqueue and dequeue in O(1) time.

A resizable array can support amortized O(1) stack pushes. But implementing a queue by shifting every remaining array element after removing the first item makes that removal O(n).

A circular buffer or suitable deque avoids those repeated shifts. Princeton's stacks and queues chapter includes linked and array implementations that make these tradeoffs concrete.

When explaining complexity, name the operation and the implementation assumption. “It's a queue” does not by itself prove that removing an item is constant time.

What about a deque or priority queue?

A deque supports insertion and removal at both ends, so it can supply stack or queue behavior. A priority queue selects by priority, such as the smallest value, rather than by arrival order.

If your task repeatedly asks for the largest or smallest candidate, read about heaps and top-k elements. FIFO and LIFO are not the only useful ordering rules.

Practice predicting the next item

Push 4, push 9, pop once, then push 6. What does the next pop return?

6. The earlier pop removed 9, leaving 4. Adding 6 made it the most recent item.

Questions this small still test a precise mental model. Hourly DSA makes room for short concept checks during the day; follow one with a trace in which you change just one operation and predict the new result.

Trace how queue and stack behavior change the order in which a graph is explored.

BFS vs. DFS: Which Graph Traversal Should You Use? →