DSA Interview Question

What is the difference between a stack and a queue?

Updated 2026-08-16 · Beginner friendly
Quick answer

A stack follows last in first out, so the most recently added item is removed first, like a pile of plates. A queue follows first in first out, so the earliest added item is removed first, like a line at a counter. Stacks use push and pop, queues use enqueue and dequeue.

Key takeaways
  • A stack is last in first out, using push and pop, like a pile of plates.
  • A queue is first in first out, using enqueue and dequeue, like a line at a counter.
  • Stacks power undo, function calls, and expression evaluation, while queues power scheduling and breadth first search.

Stack: last in first out

You add and remove from the same end, called the top. The last plate you put on the pile is the first one you take off. Stacks power the undo button, function call handling, and expression evaluation.

stack<int> st;
st.push(1);        // push
st.push(2);
st.pop();          // removes 2 (last in)
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1);     // push
stack.push(2);
stack.pop();       // removes 2 (last in)
stack = []
stack.append(1)   # push
stack.append(2)
stack.pop()       # removes 2 (last in)

Queue: first in first out

You add at the back and remove from the front. The first person in line is served first. Queues power task scheduling, printers, and breadth first search.

queue<int> q;
q.push(1);         // enqueue
q.push(2);
q.pop();           // removes 1 (first in)
Queue<Integer> q = new LinkedList<>();
q.offer(1);        // enqueue
q.offer(2);
q.poll();          // removes 1 (first in)
from collections import deque
q = deque()
q.append(1)       # enqueue
q.append(2)
q.popleft()       # removes 1 (first in)
In the interview

A common follow up is a real use case. Say a stack for undo and function calls, and a queue for scheduling and breadth first search. Concrete examples show you understand why they exist.

Frequently asked questions

Where is a stack used in recursion?

The call stack that tracks paused function calls is itself a stack. The most recent call runs and returns first, which is last in first out behaviour.

What is a circular queue?

It is a queue that reuses freed space at the front by wrapping the end around to the start, which avoids wasting memory in a fixed size buffer.

Want the full DSA guide?

Read every DSA concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the DSA guide All DSA questions