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

Data Structures & Algorithms · Basic · question 3 of 100

Can you define a stack and a queue? Explain how they differ from each other.?

📕 Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers — PDF + EPUB for $5

A stack and a queue are two popular abstract data types that are used to store and retrieve elements in a particular order. Both of these data structures have their own unique properties and are suited for specific use cases.

A stack is a collection of elements that can be accessed only from one end, known as the top of the stack. The Last-In-First-Out (LIFO) principle governs stacks, which means that the last element added to the stack will be the first one to be removed. The most common operations performed on a stack are push and pop. Push adds an element to the top of the stack, whereas pop removes and returns the top element from the stack.

Here is an example of how to implement a stack in Java:

class Stack {
    private int[] elements;
    private int top;
    
    public Stack(int size) {
        elements = new int[size];
        top = -1;
    }
    
    public void push(int value) {
        elements[++top] = value;
    }
    
    public int pop() {
        return elements[top--];
    }
}

A queue, on the other hand, is a collection of elements that can be accessed from both ends. The First-In-First-Out (FIFO) principle governs queues, which means that the first element added to the queue will be the first one to be removed. The most common operations performed on a queue are enqueue and dequeue. Enqueue adds an element to the end of the queue, whereas dequeue removes and returns the element from the front of the queue.

Here is an example of how to implement a queue in Java:

class Queue {
    private int[] elements;
    private int front;
    private int rear;
    
    public Queue(int size) {
        elements = new int[size];
        front = -1;
        rear = -1;
    }
    
    public void enqueue(int value) {
        if (isEmpty()) {
            front = 0;
            rear = 0;
        } else {
            rear++;
        }
        elements[rear] = value;
    }
    
    public int dequeue() {
        int value = elements[front];
        if (front == rear) {
            front = -1;
            rear = -1;
        } else {
            front++;
        }
        return value;
    }
    
    public boolean isEmpty() {
        return front == -1 && rear == -1;
    }
}

The main difference between a stack and a queue is the order in which elements are added and removed. In a stack, the last element added is the first one to be removed, whereas in a queue, the first element added is the first one to be removed. Additionally, stacks are best suited for problems where we need to keep track of the most recently added elements, whereas queues are best suited for problems where we need to process elements in the order they were added.

In summary, stacks and queues are two popular abstract data types that are used to store and retrieve elements in a particular order. Both of these data structures have their own unique properties and are suited for specific use cases.

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

All 100 Data Structures & Algorithms questions · All topics