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 Β· Intermediate Β· question 25 of 100

What are the primary operations of a stack, and how do they work?

πŸ“• Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers β€” PDF + EPUB for $5

A stack is a data structure that allows elements to be added and removed in a last-in, first-out (LIFO) order. The primary operations of a stack are push and pop.

The push operation adds an element to the top of the stack. It takes a single argument, the element to be added, and adds it to the top of the stack. The new element becomes the top element of the stack, and all other elements are shifted down by one position. For example, consider the following implementation of a stack using an array in Java:

public class Stack {
    private int[] data;
    private int top;

    public Stack(int capacity) {
        data = new int[capacity];
        top = -1;
    }

    public void push(int element) {
        if (top == data.length - 1) {
            throw new IllegalStateException("Stack overflow");
        }
        top++;
        data[top] = element;
    }
}

In this implementation, the push method takes an integer argument element and adds it to the top of the stack. It first checks if the stack is full by comparing the top index to the length of the array, and throws an exception if the stack is full. Otherwise, it increments the top index and sets the value of the new top element to element.

The pop operation removes the top element from the stack. It takes no arguments, and returns the element that was removed. The top element of the stack is removed, and the element immediately below it becomes the new top element. For example, consider the following implementation of a stack using an array in Java:

public class Stack {
    private int[] data;
    private int top;

    public Stack(int capacity) {
        data = new int[capacity];
        top = -1;
    }

    public int pop() {
        if (top == -1) {
            throw new NoSuchElementException("Stack underflow");
        }
        int element = data[top];
        top--;
        return element;
    }
}

In this implementation, the pop method removes the top element from the stack and returns it. It first checks if the stack is empty by comparing the top index to -1, and throws an exception if the stack is empty. Otherwise, it retrieves the value of the top element and decrements the top index.

Other operations that may be supported by a stack include peek, which retrieves the top element without removing it, and isEmpty, which checks if the stack is empty. These operations are commonly used in conjunction with the push and pop operations to manipulate the elements in the stack.

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