Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lifo
stack
fifo
queue

What is the difference between LIFO and FIFO?

Behzad Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

LIFO and FIFO are two techniques of maintaining data in a data structure. They also determine the sequence of entry/exit of data into a data structure. Let’s look at both of the techniques.

LIFO

LIFO stands for Last-in, First-out. In the LIFO technique, as the name suggests, the data which is entered in last will get to exit first.

In data structures, the stack data structure uses the LIFO technique for maintaining the data. Let’s see the graphical representation of the stack data structure.

If we push an element into the stack and pop an element, the data structure looks like this.

You can see that the point of entry and exit of the data is the same. As a result, the data which was entered last will be pulled out first.

You can also notice stack data structure and the LIFO principle in real life. A stack of dish plates is a real-world example of stack data structure. The new plate is to be placed at the top, and eventually will be picked first, compared to the plates underneath it.

FIFO

FIFO stands for First-in, First-out. In the FIFO technique, as the name suggests, the data entered first will also exit first.

In data structures, the queue data structure uses the FIFO technique for maintaining the data. Let’s see the graphical representation of the queue data structure.

Now, if we enqueue and dequeue an element, it looks like this.

You can see that the point of entry is on one side, and the exit point is on another. So, the data that gets inside first will also eventually come out first.

You can also notice the queue data structure and the FIFO principle in real life. One example is a queue of customers to a counter. The new customers join the queue at the end, while the people at the front place their order at the counter and leave the queue first.

RELATED TAGS

lifo
stack
fifo
queue

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring