Skip to main content

What is the LIFO principle in a stack?

The LIFO (Last In, First Out) principle means that the last element added is removed first.

That is:

  1. The element that enters the stack last ends up on top.
  2. When removing an element (the pop operation), it is exactly this top element that is removed.
  3. The remaining elements stay in the stack and wait their turn.

Example: If you add the elements 1, 2, 3 to the stack (in that order), they come out in reverse order when removed: 3, 2, 1.

Short Answer

Interview ready
Premium

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