Bird
Raised Fist0
DSA C++programming~20 mins

Queue Implementation Using Array in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Enqueue and Dequeue Operations
What is the output of the queue after performing these operations?

Start with an empty queue of size 5.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40.
Show the queue elements from front to rear.
DSA C++
#include <iostream>

class Queue {
  int arr[5];
  int front, rear;
public:
  Queue() { front = -1; rear = -1; }
  bool isEmpty() { return front == -1; }
  bool isFull() { return rear == 4; }
  void enqueue(int x) {
    if (isFull()) return;
    if (isEmpty()) front = 0;
    arr[++rear] = x;
  }
  void dequeue() {
    if (isEmpty()) return;
    if (front == rear) front = rear = -1;
    else front++;
  }
  void printQueue() {
    if (isEmpty()) {
      std::cout << "Queue is empty";
      return;
    }
    for (int i = front; i <= rear; i++) {
      std::cout << arr[i] << " ";
    }
  }
};

int main() {
  Queue q;
  q.enqueue(10);
  q.enqueue(20);
  q.enqueue(30);
  q.dequeue();
  q.enqueue(40);
  q.printQueue();
  return 0;
}
A10 20 30 40
B20 30 40
C30 40
DQueue is empty
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element and front moves forward.
❓ Predict Output
intermediate
2:00remaining
Queue Full Condition Check
Given a queue of size 3 implemented using array, what will be the output after these operations?

Enqueue 5, 10, 15.
Attempt to enqueue 20.
Print queue elements.
DSA C++
#include <iostream>

class Queue {
  int arr[3];
  int front, rear;
public:
  Queue() { front = -1; rear = -1; }
  bool isEmpty() { return front == -1; }
  bool isFull() { return rear == 2; }
  void enqueue(int x) {
    if (isFull()) {
      std::cout << "Queue is full" << std::endl;
      return;
    }
    if (isEmpty()) front = 0;
    arr[++rear] = x;
  }
  void printQueue() {
    if (isEmpty()) {
      std::cout << "Queue is empty" << std::endl;
      return;
    }
    for (int i = front; i <= rear; i++) {
      std::cout << arr[i] << " ";
    }
  }
};

int main() {
  Queue q;
  q.enqueue(5);
  q.enqueue(10);
  q.enqueue(15);
  q.enqueue(20);
  q.printQueue();
  return 0;
}
A
Queue is full
5 10 15 
B5 10 15 20
C
Queue is full
5 10 15 20 
DQueue is empty
Attempts:
2 left
💡 Hint
Check the isFull condition before enqueue.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Dequeue Method
What is the bug in this dequeue method of a queue implemented using array?

void dequeue() {
if (front == -1) return;
if (front == rear) front = rear = -1;
else front--;
}

Choose the correct explanation.
DSA C++
void dequeue() {
  if (front == -1) return;
  if (front == rear) front = rear = -1;
  else front--;
}
AShould reset rear to -1 only, front remains same.
BNo bug, code correctly removes front element.
CDequeue should increment rear, not front.
DDecrementing front moves it backward, should increment to remove front element.
Attempts:
2 left
💡 Hint
Think about which direction front moves when removing an element.
🧠 Conceptual
advanced
1:30remaining
Why Circular Queue is Better Than Simple Array Queue?
Which of the following is the main advantage of using a circular queue over a simple array-based queue?
AIt allows reuse of empty spaces after dequeue operations without shifting elements.
BIt uses less memory than a simple array queue.
CIt automatically sorts elements in the queue.
DIt allows random access to elements in the queue.
Attempts:
2 left
💡 Hint
Think about what happens to space when elements are dequeued in a simple array queue.
❓ Predict Output
expert
2:30remaining
Output After Multiple Enqueue and Dequeue in Circular Queue
Consider a circular queue of size 4 implemented using array with front and rear initialized to -1.
Perform these operations:
enqueue(1), enqueue(2), enqueue(3), dequeue(), enqueue(4), enqueue(5), dequeue(), enqueue(6).
What is the queue content from front to rear?
DSA C++
#include <iostream>

class CircularQueue {
  int arr[4];
  int front, rear, size;
public:
  CircularQueue() { front = -1; rear = -1; size = 4; }
  bool isFull() { return (rear + 1) % size == front; }
  bool isEmpty() { return front == -1; }
  void enqueue(int x) {
    if (isFull()) return;
    if (isEmpty()) front = 0;
    rear = (rear + 1) % size;
    arr[rear] = x;
  }
  void dequeue() {
    if (isEmpty()) return;
    if (front == rear) front = rear = -1;
    else front = (front + 1) % size;
  }
  void printQueue() {
    if (isEmpty()) {
      std::cout << "Queue is empty";
      return;
    }
    int i = front;
    while (true) {
      std::cout << arr[i] << " ";
      if (i == rear) break;
      i = (i + 1) % size;
    }
  }
};

int main() {
  CircularQueue q;
  q.enqueue(1);
  q.enqueue(2);
  q.enqueue(3);
  q.dequeue();
  q.enqueue(4);
  q.enqueue(5);
  q.dequeue();
  q.enqueue(6);
  q.printQueue();
  return 0;
}
A4 5 6
B2 3 4 5
C3 4 5 6
DQueue is empty
Attempts:
2 left
💡 Hint
Track front and rear indices carefully after each operation using modulo arithmetic.

Practice

(1/5)
1. What is the main purpose of using an array to implement a queue?
easy
A. To store elements in a fixed-size linear order for FIFO operations
B. To store elements randomly for quick access
C. To sort elements automatically
D. To store elements in a LIFO order

Solution

  1. Step 1: Understand queue behavior

    A queue follows FIFO (First In First Out), meaning elements are processed in the order they arrive.
  2. Step 2: Role of array in queue

    An array stores elements in a fixed linear order, which helps maintain the FIFO order by using front and rear indices.
  3. Final Answer:

    To store elements in a fixed-size linear order for FIFO operations -> Option A
  4. Quick Check:

    Queue uses array for FIFO storage = C [OK]
Hint: Queue means FIFO; array stores elements linearly [OK]
Common Mistakes:
  • Confusing queue with stack (LIFO)
  • Thinking array sorts elements automatically
  • Assuming random access is main goal
2. Which of the following is the correct way to declare a queue array of size 5 in C++?
easy
A. array queue(5);
B. int queue[5];
C. int[5] queue;
D. queue int[5];

Solution

  1. Step 1: Recall C++ array declaration syntax

    In C++, arrays are declared as: type name[size]; so int queue[5]; is correct.
  2. Step 2: Check other options

    Options A, B, and C do not follow valid C++ syntax for array declaration.
  3. Final Answer:

    int queue[5]; -> Option B
  4. Quick Check:

    C++ array syntax = type name[size] [OK]
Hint: Remember C++ arrays: type name[size]; [OK]
Common Mistakes:
  • Placing size before name
  • Using wrong keywords like 'array'
  • Incorrect order of type and name
3. What will be the output after performing these operations on an empty queue of size 3 implemented using array?
enqueue(10); enqueue(20); dequeue(); enqueue(30); display();
Assume enqueue adds at rear and dequeue removes from front.
medium
A. 10 -> 30 -> null
B. 10 -> 20 -> 30 -> null
C. 30 -> null
D. 20 -> 30 -> null

Solution

  1. Step 1: Perform enqueue(10) and enqueue(20)

    Queue becomes: 10 -> 20 -> null
  2. Step 2: Perform dequeue()

    Removes 10 from front, queue becomes: 20 -> null
  3. Step 3: Perform enqueue(30)

    Add 30 at rear, queue becomes: 20 -> 30 -> null
  4. Step 4: Display queue

    Output is: 20 -> 30 -> null
  5. Final Answer:

    20 -> 30 -> null -> Option D
  6. Quick Check:

    After dequeue and enqueue, queue = 20, 30 [OK]
Hint: Track front and rear after each operation [OK]
Common Mistakes:
  • Not removing element on dequeue
  • Adding new element at front instead of rear
  • Confusing order of elements
4. Identify the error in this queue implementation snippet using array:
int queue[3], front = -1, rear = -1;
void enqueue(int x) {
  if (rear == 3) {
    cout << "Queue Full";
    return;
  }
  rear++;
  queue[rear] = x;
  if (front == -1) front = 0;
}
medium
A. front should be incremented in enqueue
B. rear should be initialized to 0
C. Condition should be rear == 2 to check full queue
D. No error, code is correct

Solution

  1. Step 1: Understand array indexing for size 3

    Array indices are 0, 1, 2. So rear should not exceed 2.
  2. Step 2: Check condition for full queue

    Condition rear == 3 is wrong because index 3 is out of bounds. It should be rear == 2.
  3. Final Answer:

    Condition should be rear == 2 to check full queue -> Option C
  4. Quick Check:

    Array max index = size-1 = 2 [OK]
Hint: Array max index = size - 1, check boundary carefully [OK]
Common Mistakes:
  • Using size instead of size-1 for index check
  • Incrementing front in enqueue
  • Initializing rear incorrectly
5. Given a queue implemented using an array of size 4, initially empty, what will be the state of the queue after these operations?
enqueue(5); enqueue(10); dequeue(); enqueue(15); enqueue(20); dequeue(); enqueue(25); display();
Assume no circular queue and enqueue fails if rear reaches size-1.
hard
A. 15 -> 20 -> null
B. 10 -> 15 -> 20 -> 25 -> null
C. 15 -> 20 -> 25 -> null
D. 5 -> 10 -> 15 -> 20 -> null

Solution

  1. Step 1: Perform enqueue(5), enqueue(10)

    Queue: 5 -> 10 -> null; front=0, rear=1
  2. Step 2: Perform dequeue()

    Remove 5; Queue: 10 -> null; front=1, rear=1
  3. Step 3: Perform enqueue(15), enqueue(20)

    Queue: 10 -> 15 -> 20 -> null; front=1, rear=3
  4. Step 4: Perform dequeue()

    Remove 10; Queue: 15 -> 20 -> null; front=2, rear=3
  5. Step 5: Perform enqueue(25)

    rear=3 (max index), cannot enqueue 25; operation fails, queue unchanged
  6. Step 6: Display queue

    Queue elements from front=2 to rear=3: 15 -> 20 -> null
  7. Final Answer:

    15 -> 20 -> null -> Option A
  8. Quick Check:

    Check enqueue failure at full rear index [OK]
Hint: No circular queue means no wrap-around; watch rear limit [OK]
Common Mistakes:
  • Assuming enqueue works after rear reaches max
  • Not updating front after dequeue
  • Confusing queue order after operations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard