Skip to main content

How does a stack differ from a queue?

The main difference is in the order elements are processed:

  • A stack works on the LIFO (Last In, First Out) principle: last in, first out. Example: a stack of books, to take the bottom one, you need to remove the ones on top.
  • A queue works on the FIFO (First In, First Out) principle: first in, first out. Example: a line at a store, whoever got there first is served first.

By operations:

  • In a stack, adding and removing happen at one end (the top).
  • In a queue: adding happens at the tail, and removing happens at the head.

In short: a stack is like a "history of actions", a queue is like a "waiting line".

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.