Stack vs queue in one pictureCore idea
Stack: last in, first outHow it works
The last element pushed is the first popped (LIFO). Think of a stack of plates: you add and remove from the top.
const stack: number[] = [];
stack.push(1); // [1]
stack.push(2); // [1, 2]
stack.push(3); // [1, 2, 3]
stack.pop(); // returns 3, stack is [1, 2]
stack[stack.length - 1]; // peek: returns 2Balanced parentheses
Push opening brackets, pop on each closing one. Balanced if the stack is empty at the end.
Next greater element
A monotonic stack, explained below.
Evaluate expressions
Push operands, pop them when you reach an operator.
DFS traversal
Recursive DFS uses the call stack, which is a stack.
Queue: first in, first outHow it works
The first element added is the first removed (FIFO). Think of a line at a shop: the first person in line is served first.
const queue: number[] = [];
queue.push(1); // enqueue: [1]
queue.push(2); // [1, 2]
queue.shift(); // dequeue: returns 1, queue is [2]BFS traversal
BFS uses a queue to explore a graph level by level.
Task scheduling
Round-robin scheduling handles tasks in FIFO order.
Sliding window maximum
A deque keeps the candidates for the current maximum.
Monotonic stackPattern
A stack whose values are always increasing (or always decreasing) from bottom to top. Each new element pops every element it beats, and those popped elements have just found their “next greater element”. Every index is pushed and popped once, so the whole scan is O(n).
Next greater element for [2, 1, 5]
function nextGreaterElement(nums: number[]): number[] {
const result = new Array(nums.length).fill(-1);
const stack: number[] = []; // stores indices
for (let i = 0; i < nums.length; i++) {
while (stack.length && nums[stack[stack.length - 1]] < nums[i]) {
result[stack.pop()!] = nums[i];
}
stack.push(i);
}
return result;
}Deque (double-ended queue)Core idea
A deque adds and removes at both ends in O(1). It can act as a stack or a queue, and it is the tool behind the sliding window maximum.
| Structure | Add | Remove | Typical use |
|---|---|---|---|
| Stack | Top | Top | DFS, brackets, monotonic stack |
| Queue | Back | Front | BFS, scheduling |
| Deque | Both ends | Both ends | Sliding window maximum |
Common mistakesPitfalls
In the interviewIn the interview
Frequently asked questionsFAQ
What is the difference between a stack and a queue?
A stack is last-in-first-out, like a stack of plates, with push and pop from the same end. A queue is first-in-first-out, like a ticket line, with insert at the back and removal from the front. Both support their core operations in O(1).
What is a monotonic stack and when do you use it?
A monotonic stack keeps its elements in sorted order by popping smaller or larger entries before pushing, so it maintains useful boundaries as you scan left to right. It is the standard O(n) solution for next-greater-element, largest-rectangle-in-histogram, and daily-temperatures, where a plain stack would rescan the same regions repeatedly.
Which queue variant is best for breadth-first search?
A plain first-in-first-out queue gives the standard BFS level order. A deque, or double-ended queue, is used when some entries must be pushed to the front, as in 0-1 BFS for shortest paths on weighted graphs. Pointer-based queues are preferred over shifting array queues at scale.

Now explain stack and queue to me out loud
I'll give you interview problems and ask follow-ups, like a real interviewer.

