How does a queue differ from a stack?
A queue and a stack differ in the order in which elements are processed:
- Stack: the LIFO (Last In, First Out) principle: last added, first out. Example: a stack of plates: you take the top one first.
- Queue: the FIFO (First In, First Out) principle: first added, first out. Example: a line in a store: whoever arrived earlier leaves earlier.
Technically:
- In a stack, additions and removals happen at one end (the top).
- In a queue, additions happen at the tail, removals at the head.
Summary: a stack keeps order "backward", a queue keeps order "forward".
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.