WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Software Engineering · Basic · question 6 of 100

What is a stack and a queue? Explain their main differences and provide examples of when to use each.?

📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

Stack and queue are two fundamental data structures in computer science. Both are used for storing and manipulating data, but they differ in their principles of operation and functionality. In this answer, we’ll go over each of them in details and see their main differences.

A **stack** is a linear data structure that follows the LIFO (Last-In, First-Out) principle. It consists of a sequence of elements, where the last element added is the first one to be removed. The basic operations that can be performed on a stack are:

- Push: adds an element to the top of the stack.

- Pop: removes the element at the top of the stack.

- Peek: returns the element at the top of the stack without removing it.

- IsEmpty: checks whether the stack is empty.

A real-life example of a stack is a pile of plates stacked on top of one another. The top plate can be added or removed easily, while the others can only be accessed by removing the top ones. Another example is the **call stack** in computer programming, which is used for keeping track of function calls and their return addresses.

Here’s an example of a stack implementation in Python:

class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[-1]

    def isEmpty(self):
        return len(self.items) == 0

A **queue**, on the other hand, is a linear data structure that follows the FIFO (First-In, First-Out) principle. It also consists of a sequence of elements, but the first element added is the first one to be removed. The basic operations that can be performed on a queue are:

- Enqueue: adds an element to the back of the queue.
- Dequeue: removes the element at the front of the queue.
- Peek: returns the element at the front of the queue without removing it.
- IsEmpty: checks whether the queue is empty.

A real-life example of a queue is a line of people waiting to buy tickets at a cinema. The first person who joins the line is the first one to get the ticket, while the others have to wait their turn. Another example is the **task queue** in computer programming, which is used for executing tasks in the order they are received.

Here’s an example of a queue implementation in Python:

class Queue:
    def __init__(self):
        self.items = []

    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        return self.items.pop(0)

    def peek(self):
        return self.items[0]

    def isEmpty(self):
        return len(self.items) == 0

So, what are the main differences between a stack and a queue?

- The main principle of operation. A stack follows the LIFO principle, while a queue follows the FIFO principle.
- The insertion and removal order. In a stack, elements are inserted and removed from the top. In a queue, elements are inserted at the back and removed from the front.
- The main use cases. A stack is used when we need to keep track of the order of function calls or undo/redo operations, for example. A queue is used when we need to process data in the order it is received, such as in a task queue.

In conclusion, both stack and queue are important data structures with their own principles of operation and use cases. Understanding their differences and knowing when to use each of them is essential for writing efficient and reliable code.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Software Engineering interview — then scores it.
📞 Practice Software Engineering — free 15 min
📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

All 100 Software Engineering questions · All topics