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 26 of 100

Describe the primary operations of a queue, and explain how a circular queue can be implemented.?

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

A queue is a data structure that allows elements to be added at the rear and removed from the front in a first-in, first-out (FIFO) order. The primary operations of a queue are enqueue and dequeue.

The enqueue operation adds an element to the rear of the queue. It takes a single argument, the element to be added, and adds it to the end of the queue. For example, consider the following implementation of a queue using an array in Java:

public class Queue {
    private int[] data;
    private int front, rear;

    public Queue(int capacity) {
        data = new int[capacity];
        front = -1;
        rear = -1;
    }

    public void enqueue(int element) {
        if (rear == data.length - 1) {
            throw new IllegalStateException("Queue overflow");
        }
        rear++;
        data[rear] = element;
        if (front == -1) {
            front = 0;
        }
    }
}

In this implementation, the enqueue method takes an integer argument element and adds it to the rear of the queue. It first checks if the queue is full by comparing the rear index to the length of the array, and throws an exception if the queue is full. Otherwise, it increments the rear index and sets the value of the new rear element to element. If this is the first element to be added to the queue, it sets the front index to 0.

The dequeue operation removes the element at the front of the queue. It takes no arguments, and returns the element that was removed. The front element of the queue is removed, and the element immediately behind it becomes the new front element. For example, consider the following implementation of a queue using an array in Java:

public class Queue {
    private int[] data;
    private int front, rear;

    public Queue(int capacity) {
        data = new int[capacity];
        front = -1;
        rear = -1;
    }

    public int dequeue() {
        if (front == -1 || front > rear) {
            throw new NoSuchElementException("Queue underflow");
        }
        int element = data[front];
        front++;
        return element;
    }
}

In this implementation, the dequeue method removes the front element from the queue and returns it. It first checks if the queue is empty by comparing the front index to -1, or if the front index is greater than the rear index, and throws an exception if the queue is empty. Otherwise, it retrieves the value of the front element and increments the front index.

A circular queue is a variation of the queue data structure in which the rear and front indices are connected, allowing the queue to wrap around to the beginning of the array when it reaches the end. This allows the queue to reuse the space at the beginning of the array once the elements at the front of the queue have been dequeued, and can be useful in cases where the size of the queue is fixed and a dynamic resizing operation is not desirable.

Here is an example implementation of a circular queue using an array in Java:

public class CircularQueue {
    private int[] data;
    private int front, rear, size;

    public CircularQueue(int capacity) {
        data = new int[capacity];
        front = 0;
        rear = -1;
        size = 0;
    }

    public void enqueue(int element) {
        if (size == data.length) {
            throw new IllegalStateException("Queue overflow");
        }
        rear = (rear + 1) % data.length;
        data[rear] = element;
        size++;
    }
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