Bird
Raised Fist0
DSA C++programming~20 mins

Queue Implementation Using Linked List 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 printed state of the queue after the following operations?

Enqueue 10, Enqueue 20, Dequeue, Enqueue 30
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class Queue {
    Node* front;
    Node* rear;
public:
    Queue() : front(nullptr), rear(nullptr) {}
    void enqueue(int val) {
        Node* newNode = new Node(val);
        if (!rear) {
            front = rear = newNode;
            return;
        }
        rear->next = newNode;
        rear = newNode;
    }
    void dequeue() {
        if (!front) return;
        Node* temp = front;
        front = front->next;
        if (!front) rear = nullptr;
        delete temp;
    }
    void print() {
        Node* temp = front;
        while (temp) {
            std::cout << temp->data << " -> ";
            temp = temp->next;
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    Queue q;
    q.enqueue(10);
    q.enqueue(20);
    q.dequeue();
    q.enqueue(30);
    q.print();
    return 0;
}
A10 -> 30 -> null
B20 -> 30 -> null
C10 -> 20 -> 30 -> null
D30 -> null
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element.
🧠 Conceptual
intermediate
1:30remaining
Queue Empty State Check
Which condition correctly checks if the queue implemented using linked list is empty?
Aif (front == nullptr) { /* queue is empty */ }
Bif (rear == nullptr) { /* queue is empty */ }
Cif (front == rear) { /* queue is empty */ }
Dif (front->next == nullptr) { /* queue is empty */ }
Attempts:
2 left
💡 Hint
Empty queue means no front node.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Dequeue Method
What error will occur when running this dequeue method if the queue has only one element?
DSA C++
void dequeue() {
    if (!front) return;
    Node* temp = front;
    front = front->next;
    delete temp;
}
ADangling rear pointer causing undefined behavior
BNo error, works fine
CSegmentation fault due to deleting null pointer
DMemory leak because node is not deleted
Attempts:
2 left
💡 Hint
Consider what happens to rear when the last element is dequeued.
❓ Predict Output
advanced
2:00remaining
Output After Multiple Enqueue and Dequeue
What is the queue state after these operations?

Enqueue 5, Enqueue 15, Enqueue 25, Dequeue, Dequeue, Enqueue 35
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class Queue {
    Node* front;
    Node* rear;
public:
    Queue() : front(nullptr), rear(nullptr) {}
    void enqueue(int val) {
        Node* newNode = new Node(val);
        if (!rear) {
            front = rear = newNode;
            return;
        }
        rear->next = newNode;
        rear = newNode;
    }
    void dequeue() {
        if (!front) return;
        Node* temp = front;
        front = front->next;
        if (!front) rear = nullptr;
        delete temp;
    }
    void print() {
        Node* temp = front;
        while (temp) {
            std::cout << temp->data << " -> ";
            temp = temp->next;
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    Queue q;
    q.enqueue(5);
    q.enqueue(15);
    q.enqueue(25);
    q.dequeue();
    q.dequeue();
    q.enqueue(35);
    q.print();
    return 0;
}
A15 -> 25 -> 35 -> null
B35 -> null
C25 -> 35 -> null
D25 -> null
Attempts:
2 left
💡 Hint
Dequeue removes from front, track carefully.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Queue Operations Using Linked List
What is the time complexity of enqueue and dequeue operations in a queue implemented using a linked list with front and rear pointers?
ABoth enqueue and dequeue are O(n)
BEnqueue is O(n), dequeue is O(1)
CEnqueue is O(1), dequeue is O(n)
DBoth enqueue and dequeue are O(1)
Attempts:
2 left
💡 Hint
Think about how front and rear pointers help.

Practice

(1/5)
1. What is the main advantage of implementing a queue using a linked list instead of an array?
easy
A. Dynamic size without fixed capacity
B. Faster access to middle elements
C. Uses less memory always
D. Simpler to implement than arrays

Solution

  1. Step 1: Understand queue size limitation in arrays

    Arrays have fixed size, so queues implemented with arrays have a maximum capacity.
  2. Step 2: Linked list dynamic size

    Linked lists can grow or shrink dynamically, so queues using linked lists do not have fixed capacity limits.
  3. Final Answer:

    Dynamic size without fixed capacity -> Option A
  4. Quick Check:

    Linked list queues grow dynamically = D [OK]
Hint: Linked list queues grow dynamically, arrays don't [OK]
Common Mistakes:
  • Thinking linked lists use less memory always
  • Confusing access speed advantages
  • Assuming linked lists are simpler to implement
2. Which of the following is the correct way to declare a node structure for a queue using a linked list in C++?
easy
A. struct Node { int data; Node* next; };
B. struct Node { int data; Node next; };
C. struct Node { int data; Node* prev; };
D. struct Node { int data; int next; };

Solution

  1. Step 1: Understand node structure for linked list

    A node must contain data and a pointer to the next node.
  2. Step 2: Analyze options

    struct Node { int data; Node* next; }; correctly uses a pointer to next node. struct Node { int data; Node next; }; uses next as object, which is invalid. struct Node { int data; Node* prev; }; uses prev pointer, which is for doubly linked list. struct Node { int data; int next; }; uses int for next, which is incorrect.
  3. Final Answer:

    struct Node { int data; Node* next; }; -> Option A
  4. Quick Check:

    Node with data and next pointer = A [OK]
Hint: Node must have pointer to next node, not object or int [OK]
Common Mistakes:
  • Using object instead of pointer for next
  • Confusing singly and doubly linked list nodes
  • Using int instead of pointer for next
3. What will be the output after performing these operations on an empty queue implemented using linked list?
enqueue(10); enqueue(20); dequeue(); enqueue(30); dequeue();

Print the queue elements from front to rear.
medium
A. 20 -> 30 -> null
B. 10 -> 20 -> 30 -> null
C. 30 -> null
D. 10 -> 30 -> null

Solution

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

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

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

    Queue becomes: 20 -> 30 -> null
  4. Step 4: Perform dequeue()

    Removes 20, queue becomes: 30 -> null
  5. Final Answer:

    30 -> null -> Option C
  6. Quick Check:

    After operations queue front is 30 [OK]
Hint: Track enqueue/dequeue stepwise to find final queue [OK]
Common Mistakes:
  • Forgetting to remove front on dequeue
  • Appending dequeue element instead of removing
  • Mixing order of operations
4. Identify the error in this dequeue function for a queue implemented using linked list:
int dequeue() {
  if (front == nullptr) return -1;
  Node* temp = front;
  front = front->next;
  int val = temp->data;
  delete temp;
  return val;
}
medium
A. No error, function is correct
B. Does not update rear when queue becomes empty
C. Deletes front node before accessing data
D. Returns wrong value after deletion

Solution

  1. Step 1: Analyze dequeue steps

    Function removes front node and returns its data correctly.
  2. Step 2: Check rear pointer update

    If after dequeue front becomes nullptr (empty queue), rear should also be set to nullptr but this is missing.
  3. Final Answer:

    Does not update rear when queue becomes empty -> Option B
  4. Quick Check:

    Rear must be nullptr if queue empty [OK]
Hint: Update rear to nullptr when queue becomes empty [OK]
Common Mistakes:
  • Ignoring rear pointer update on empty queue
  • Deleting node before reading data
  • Returning wrong value after deletion
5. You want to implement a function reverseQueue() that reverses the elements of a queue implemented using a linked list without using extra arrays or STL containers. Which approach is best?
hard
A. Use recursion to dequeue elements and enqueue them back in reverse order
B. Traverse linked list and swap data of front and rear nodes repeatedly
C. Create a new linked list and insert elements at front while dequeuing
D. Use two pointers to reverse links between nodes iteratively

Solution

  1. Step 1: Understand reversing linked list nodes

    Reversing links between nodes iteratively changes the queue order without extra space.
  2. Step 2: Analyze options

    Use recursion to dequeue elements and enqueue them back in reverse order uses recursion but may use call stack (extra space). Traverse linked list and swap data of front and rear nodes repeatedly swaps data but is inefficient and complex. Create a new linked list and insert elements at front while dequeuing creates new list (extra space). Use two pointers to reverse links between nodes iteratively reverses links in place efficiently.
  3. Final Answer:

    Use two pointers to reverse links between nodes iteratively -> Option D
  4. Quick Check:

    In-place link reversal is best for linked list queue reverse [OK]
Hint: Reverse links in place with two pointers for best solution [OK]
Common Mistakes:
  • Using extra arrays or containers
  • Swapping data instead of links
  • Using recursion causing extra space
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