Data Structure Queue Practice Questions with Solutions

Introductions

A queue is a linear data structure that follows the FIFO (First In, First Out) principle. These practice questions focus on solving queue problems instead of only learning definitions. You will practice enqueue, dequeue, front, rear, checking an empty queue, searching, reversing, and implementing a queue using JavaScript. The examples start with simple operations and gradually introduce more practical problems. Data Structure Queue Practice questions with solutions help to understand the concepts.

Question 1: Add Elements to a Queue

Question

Create an empty queue and add the values 10, 20, and 30 using the enqueue operation. Print the queue.

Solution

In a queue, a new element is added at the rear.

Start with an empty queue:

let queue = [];

Add 10:

queue.push(10);

Queue:

Front → 10 ← Rear

Add 20:

queue.push(20);

Queue:

Front → 10 → 20 ← Rear

Add 30:

queue.push(30);

Now:

Front → 10 → 20 → 30 ← Rear

Complete code:

let queue = [];

queue.push(10);
queue.push(20);
queue.push(30);

console.log(queue);

Output

[10, 20, 30]

Answer

The queue contains:

Front → 10 → 20 → 30 ← Rear

The first element added, 10, is at the front.


Question 2: Remove an Element from a Queue

Question

Given the queue:

Front → 10 → 20 → 30 → 40 ← Rear

Remove the front element and print the remaining queue.

Solution

In a queue, the element at the front is removed first.

With a JavaScript array, shift() removes the first element.

let queue = [10, 20, 30, 40];

let removed = queue.shift();

console.log("Removed:", removed);
console.log("Queue:", queue);

The process is:

Before:
Front → 10 → 20 → 30 → 40

shift()

10 is removed

After:
Front → 20 → 30 → 40

Output

Removed: 10
Queue: [20, 30, 40]

Answer

10 is removed because it was the first element in the queue.


Question 3: Find the Front Element

Question

Find the front element of this queue without removing it:

Front → 10 → 20 → 30 → 40 → 50 ← Rear

Solution

The front element is the first element of the array.

We can access it using:

queue[0]

Code:

let queue = [10, 20, 30, 40, 50];

let front = queue[0];

console.log(front);

The queue remains unchanged.

Front → 10 → 20 → 30 → 40 → 50 ← Rear

Output

10

Answer

The front element is 10.


Question 4: Find the Rear Element

Question

Find the rear element of:

Front → 10 → 20 → 30 → 40 → 50 ← Rear

without removing it.

Solution

The rear element is the last element of the array.

We can access it using:

queue[queue.length - 1]

Code:

let queue = [10, 20, 30, 40, 50];

let rear = queue[queue.length - 1];

console.log(rear);

Output

50

Answer

The rear element is 50.

The queue is not changed.


Question 5: Process a Queue Until a Specific Person is Reached

Question

A queue contains:

Front → Rahul → Priya → Amit → Neha → Rohan ← Rear

Process the queue one person at a time. Stop processing when Amit reaches the front. Print the names that were processed before Amit.

Solution

Create the queue:

let queue = ["Rahul", "Priya", "Amit", "Neha", "Rohan"];

while (queue.length > 0) {
    let person = queue.shift();

    if (person === "Amit") {
        break;
    }

    console.log(person);
}

Step by step:

Rahul → processed
Priya → processed
Amit  → stop

Amit is not printed because the program stops when Amit reaches the front.

Output

Rahul
Priya

Answer

Rahul and Priya are processed before Amit is reached.


Question 6: Remove All Elements from a Queue

Question

Remove and print all elements from:

Front → 10 → 20 → 30 → 40 ← Rear

Solution

A queue follows FIFO, so elements should be removed in this order:

10 → 20 → 30 → 40

We can repeatedly use shift() until the queue becomes empty.

let queue = [10, 20, 30, 40];

while (queue.length > 0) {
    console.log(queue.shift());
}

Step by step:

Initial:
[10, 20, 30, 40]

shift() → 10
[20, 30, 40]

shift() → 20
[30, 40]

shift() → 30
[40]

shift() → 40
[]

Output

10
20
30
40

Answer

The elements are removed in the same order in which they were inserted.


Question 7: Search for an Element in a Queue

Question

Search for 30 in:

Front → 10 → 20 → 30 → 40 → 50 ← Rear

Print "Found" if the value exists.

Solution

We can check every element using a loop.

let queue = [10, 20, 30, 40, 50];

let target = 30;
let found = false;

for (let i = 0; i < queue.length; i++) {
    if (queue[i] === target) {
        found = true;
        break;
    }
}

if (found) {
    console.log("Found");
} else {
    console.log("Not Found");
}

The search works like this:

10 → Not Found
20 → Not Found
30 → Found

Output

Found

Answer

The value 30 exists in the queue.


Question 8: Find the Maximum Element in a Queue

Question

Find the largest element in this queue:

Front → 25 → 70 → 15 → 40 → 10 ← Rear

Solution

Create the queue:

let queue = [25, 70, 15, 40, 10];

Assume the first element is the maximum:

let max = queue[0];

Compare the remaining elements:

for (let i = 1; i < queue.length; i++) {
    if (queue[i] > max) {
        max = queue[i];
    }
}

The comparison works like this:

25 → max = 25
70 → max = 70
15 → max = 70
40 → max = 70
10 → max = 70

Complete code:

let queue = [25, 70, 15, 40, 10];

let max = queue[0];

for (let i = 1; i < queue.length; i++) {
    if (queue[i] > max) {
        max = queue[i];
    }
}

console.log(max);

Output

70

Answer

The maximum element is 70.


Question 9: Reverse a Queue

Question

Reverse this queue:

[10, 20, 30, 40, 50]

Expected result:

[50, 40, 30, 20, 10]

Solution

We can use a stack to reverse the queue.

Create a stack:

let stack = [];

Remove elements from the queue and put them into the stack:

while (queue.length > 0) {
    stack.push(queue.shift());
}

Now the stack contains:

[10, 20, 30, 40, 50]

The top is 50.

Now pop the stack and put the values back into the queue:

while (stack.length > 0) {
    queue.push(stack.pop());
}

Complete code:

let queue = [10, 20, 30, 40, 50];

let stack = [];

while (queue.length > 0) {
    stack.push(queue.shift());
}

while (stack.length > 0) {
    queue.push(stack.pop());
}

console.log(queue);

The process is:

Queue:
[10, 20, 30, 40, 50]

Move to stack

Stack:
[10, 20, 30, 40, 50]

Pop from stack

Queue:
[50, 40, 30, 20, 10]

Output

[50, 40, 30, 20, 10]

Answer

The queue is successfully reversed.


Question 10: Implement a Queue Using a JavaScript Class

Question

Create a Queue class with these operations:

  • enqueue()
  • dequeue()
  • front()
  • isEmpty()

Then test the queue.

Solution

First create the class:

class Queue {
    constructor() {
        this.items = [];
    }
}

Add Enqueue Operation

The enqueue() operation adds an element at the rear.

enqueue(value) {
    this.items.push(value);
}

Add Dequeue Operation

The dequeue() operation removes the element from the front.

dequeue() {
    return this.items.shift();
}

Add Front Operation

The front() operation returns the first element without removing it.

front() {
    return this.items[0];
}

Add IsEmpty Operation

isEmpty() {
    return this.items.length === 0;
}

The complete class is:

class Queue {
    constructor() {
        this.items = [];
    }

    enqueue(value) {
        this.items.push(value);
    }

    dequeue() {
        return this.items.shift();
    }

    front() {
        return this.items[0];
    }

    isEmpty() {
        return this.items.length === 0;
    }
}

let queue = new Queue();

queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);

console.log("Front:", queue.front());

console.log("Removed:", queue.dequeue());

console.log("Front:", queue.front());

console.log("Is Empty:", queue.isEmpty());

Let’s follow the operations:

enqueue(10)
[10]

enqueue(20)
[10, 20]

enqueue(30)
[10, 20, 30]

front()
10

dequeue()
10 is removed

Queue:
[20, 30]

front()
20

Output

Front: 10
Removed: 10
Front: 20
Is Empty: false

Answer

The Queue class successfully implements the basic queue operations.

Key Takeaways

  • A queue is a linear data structure that follows FIFO (First In, First Out).
  • The enqueue() operation adds an element at the rear.
  • The dequeue() operation removes an element from the front.
  • The front() operation checks the first element without removing it.
  • The rear is the last element currently present in the queue.
  • A queue can be implemented using a JavaScript array.
  • Searching a queue generally takes O(n) time.
  • Finding the maximum element requires checking all elements and takes O(n) time.
  • Removing all elements requires processing each element.
  • A queue is useful when elements need to be processed in the same order they arrive.
  • Queues are commonly used in scheduling, task processing, printer queues, buffering, and breadth-first search.
  • When using JavaScript arrays, shift() removes the first element, but repeated shift() operations can be less efficient for large queues.
  • Understanding the difference between FIFO and LIFO is important when working with queues and stacks.

FAQs

1. What is a queue in data structures?

A queue is a linear data structure that follows the FIFO (First In, First Out) principle. The first element added is the first element removed.

2. What is the enqueue operation?

Enqueue adds a new element to the rear or end of the queue.

3. What is the dequeue operation?

Dequeue removes the element from the front of the queue.

4. What is the front of a queue?

The front is the position from which elements are removed. It normally contains the element that has been waiting the longest.

5. What is the rear of a queue?

The rear is the position where new elements are added to the queue.

6. What is the difference between a stack and a queue?

A stack follows LIFO (Last In, First Out), while a queue follows FIFO (First In, First Out). In a stack, the last added element is removed first. In a queue, the first added element is removed first.

7. Where are queues used in programming?

Queues are used in task scheduling, printer management, customer service systems, buffering, operating systems, networking, and algorithms such as Breadth-First Search (BFS).

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top