Interview Q&A / Data Structures & Algorithms

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What is the difference between a stack and a queue, with real examples?

SHORT ANSWER

A stack is last in, first out (LIFO): you add and remove at the same end. A queue is first in, first out (FIFO): you add at the back and remove from the front.

Explain it simply

A stack is a pile of plates in a mess canteen. You put a plate on top and you also take from the top, so the last plate placed is the first one used.

A queue is the line at an IRCTC ticket counter. The person who joined first is served first, and new people stand at the back.

StackQueue
OrderLIFOFIFO
Addpush (top)enqueue (back)
Removepop (top)dequeue (front)
Typical usesundo, browser back, call stack, bracket matching, DFStask scheduling, print jobs, BFS, buffering requests

In JavaScript an array works as a stack with push and pop. As a queue, push and shift work, but shift is O(n) on large arrays, so a linked list or an index pointer is better for heavy use.

Example

js
const stack = [];
stack.push("a");
stack.push("b");
console.log(stack.pop()); // b

const queue = [];
queue.push("a");
queue.push("b");
console.log(queue.shift()); // a

What to say in the interview

"A stack is LIFO and a queue is FIFO. A stack gives me undo, recursion and DFS; a queue gives me scheduling and BFS. Both support insert and remove in O(1) when implemented well, but array shift in JavaScript is O(n), so I would use a proper queue for large inputs."

Common mistakes

  • Mixing up which end is removed: the stack removes the newest, the queue removes the oldest.
  • Assuming Array.shift is O(1).

#stack#queue#data-structures

Related questions

Practise more Data Structures & Algorithms questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Data Structures & Algorithms quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.