Bird
Raised Fist0
DSA C++programming~20 mins

Enqueue Operation 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 operation on a queue
What is the printed state of the queue after performing the enqueue operations in the given code?
DSA C++
class Queue {
public:
    int arr[5];
    int front, rear;
    Queue() {
        front = -1;
        rear = -1;
    }
    void enqueue(int x) {
        if (rear == 4) {
            return; // Queue full
        }
        if (front == -1) front = 0;
        rear++;
        arr[rear] = x;
    }
    void printQueue() {
        for (int i = front; i <= rear; i++) {
            std::cout << arr[i] << " -> ";
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    Queue q;
    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    q.printQueue();
    return 0;
}
A20 -> 30 -> null
Bnull
C10 -> 20 -> 30 -> null
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Trace the enqueue steps and how front and rear pointers change.
❓ Predict Output
intermediate
2:00remaining
Enqueue operation on a circular queue
What is the output after enqueueing elements in the circular queue code below?
DSA C++
class CircularQueue {
public:
    int arr[5];
    int front, rear;
    CircularQueue() {
        front = -1;
        rear = -1;
    }
    void enqueue(int x) {
        if ((rear + 1) % 5 == front) {
            return; // Queue full
        }
        if (front == -1) front = 0;
        rear = (rear + 1) % 5;
        arr[rear] = x;
    }
    void printQueue() {
        if (front == -1) {
            std::cout << "null" << std::endl;
            return;
        }
        int i = front;
        while (true) {
            std::cout << arr[i] << " -> ";
            if (i == rear) break;
            i = (i + 1) % 5;
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    CircularQueue q;
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    q.enqueue(4);
    q.enqueue(5); // This enqueue will be ignored (queue full)
    q.printQueue();
    return 0;
}
A1 -> 2 -> 3 -> 4 -> 5 -> null
B1 -> 2 -> 3 -> 4 -> null
Cnull
D2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Remember the circular queue stops enqueue when full.
🔧 Debug
advanced
2:00remaining
Identify the error in enqueue method
What error will the following enqueue method cause when called on an empty queue?
DSA C++
void enqueue(int x) {
    if (rear == 4) {
        std::cout << "Queue full" << std::endl;
        return;
    }
    rear++;
    arr[rear] = x;
    if (front == -1) front = 0;
}
Arear increments before front check causing front to never update
BNo error, works correctly
Crear increments before front check causing out of bounds
Dfront remains -1 causing printQueue to fail
Attempts:
2 left
💡 Hint
Check the order of updating rear and front pointers.
🧠 Conceptual
advanced
1:00remaining
Maximum elements in a circular queue of size N
For a circular queue implemented with an array of size N, what is the maximum number of elements it can hold?
AN / 2
BN + 1
CN
DN - 1
Attempts:
2 left
💡 Hint
Consider how front and rear pointers are used to detect full condition.
❓ Predict Output
expert
3:00remaining
Output after multiple enqueue and dequeue operations
What is the printed state of the queue after the following operations?
DSA C++
class Queue {
public:
    int arr[5];
    int front, rear;
    Queue() {
        front = -1;
        rear = -1;
    }
    void enqueue(int x) {
        if (rear == 4) return;
        if (front == -1) front = 0;
        rear++;
        arr[rear] = x;
    }
    void dequeue() {
        if (front == -1 || front > rear) {
            return;
        }
        front++;
        if (front > rear) {
            front = -1;
            rear = -1;
        }
    }
    void printQueue() {
        if (front == -1) {
            std::cout << "null" << std::endl;
            return;
        }
        for (int i = front; i <= rear; i++) {
            std::cout << arr[i] << " -> ";
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    Queue q;
    q.enqueue(5);
    q.enqueue(10);
    q.enqueue(15);
    q.dequeue();
    q.enqueue(20);
    q.dequeue();
    q.enqueue(25);
    q.printQueue();
    return 0;
}
A15 -> 20 -> 25 -> null
B10 -> 15 -> 20 -> 25 -> null
C5 -> 10 -> 15 -> null
Dnull
Attempts:
2 left
💡 Hint
Track front and rear after each operation carefully.

Practice

(1/5)
1. What does the enqueue operation do in a queue data structure?
easy
A. Returns the size of the queue
B. Removes an element from the front of the queue
C. Checks if the queue is empty
D. Adds an element to the end of the queue

Solution

  1. Step 1: Understand the queue structure

    A queue works like a line where elements are added at the back and removed from the front.
  2. Step 2: Define the enqueue operation

    Enqueue means adding a new element to the end of the queue to wait its turn.
  3. Final Answer:

    Adds an element to the end of the queue -> Option D
  4. Quick Check:

    Enqueue = Add at end [OK]
Hint: Enqueue always adds at the back of the queue [OK]
Common Mistakes:
  • Confusing enqueue with dequeue (removal)
  • Thinking enqueue removes elements
  • Mixing enqueue with checking queue size
2. Which of the following is the correct syntax to enqueue an element 5 into a queue named q in C++ STL?
easy
A. q.push(5);
B. q.add(5);
C. q.enqueue(5);
D. q.insert(5);

Solution

  1. Step 1: Recall C++ STL queue methods

    The standard queue in C++ uses push() to add elements at the back.
  2. Step 2: Match the correct method

    enqueue is not a method in STL queue; push is used instead.
  3. Final Answer:

    q.push(5); -> Option A
  4. Quick Check:

    STL queue uses push() to enqueue [OK]
Hint: Use push() to add elements in C++ STL queue [OK]
Common Mistakes:
  • Using enqueue() which is not in STL queue
  • Using add() or insert() which are not queue methods
  • Confusing push() with pop()
3. What will be the output of the following C++ code snippet?
std::queue<int> q;
q.push(10);
q.push(20);
q.push(30);
q.pop();
std::cout << q.front();
medium
A. 10
B. Compilation error
C. 20
D. 30

Solution

  1. Step 1: Trace the enqueue operations

    Elements 10, 20, 30 are added in order: queue = [10, 20, 30]
  2. Step 2: Apply the dequeue operation

    pop() removes the front element 10, so queue = [20, 30]
  3. Step 3: Check the front element

    front() now returns 20, the new front.
  4. Final Answer:

    20 -> Option C
  5. Quick Check:

    After pop, front = 20 [OK]
Hint: pop removes front; front() shows new front [OK]
Common Mistakes:
  • Assuming front remains 10 after pop
  • Confusing pop with removing from back
  • Expecting output 30 instead of 20
4. Identify the error in the following code that tries to enqueue an element into a queue:
std::queue<int> q;
q.enqueue(15);
medium
A. Missing #include <queue>
B. enqueue is not a member function of std::queue
C. Queue must be initialized with size
D. Cannot enqueue integer values

Solution

  1. Step 1: Check std::queue methods

    std::queue does not have an enqueue method; it uses push to add elements.
  2. Step 2: Identify the error cause

    Calling enqueue causes a compilation error because the method does not exist.
  3. Final Answer:

    enqueue is not a member function of std::queue -> Option B
  4. Quick Check:

    Use push() not enqueue() in std::queue [OK]
Hint: std::queue uses push(), not enqueue() [OK]
Common Mistakes:
  • Assuming enqueue() exists in std::queue
  • Thinking queue needs size at initialization
  • Believing integers cannot be enqueued
5. You have a queue initially empty. You perform these operations:
enqueue(1)
enqueue(2)
dequeue()
enqueue(3)
dequeue()
enqueue(4)

What is the final state of the queue (front to back)?
hard
A. 3 -> 4 -> null
B. 2 -> 3 -> 4 -> null
C. 1 -> 3 -> 4 -> null
D. 4 -> null

Solution

  1. Step 1: Perform operations step-by-step

    Start empty: []
    enqueue(1): [1]
    enqueue(2): [1, 2]
    dequeue(): removes 1 -> [2]
    enqueue(3): [2, 3]
    dequeue(): removes 2 -> [3]
    enqueue(4): [3, 4]
  2. Step 2: Write final queue state

    Queue front is 3, then 4, then null (end).
  3. Final Answer:

    3 -> 4 -> null -> Option A
  4. Quick Check:

    After operations, queue = 3,4 [OK]
Hint: Track each enqueue/dequeue step carefully [OK]
Common Mistakes:
  • Forgetting to remove elements on dequeue
  • Mixing order of elements after operations
  • Assuming dequeue removes from back
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