Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Enqueue Using Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after multiple enqueues in a linked list queue
What is the printed state of the queue after enqueuing 10, 20, and 30 in order?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Queue:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self, data):
        new_node = Node(data)
        if self.rear is None:
            self.front = self.rear = new_node
            return
        self.rear.next = new_node
        self.rear = new_node

    def print_queue(self):
        current = self.front
        while current:
            print(current.data, end=' -> ')
            current = current.next
        print('null')

q = Queue()
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
q.print_queue()
Anull
B30 -> 20 -> 10 -> null
C10 -> 30 -> 20 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember, enqueue adds to the rear of the queue.
❓ Predict Output
intermediate
1:30remaining
Output after enqueuing a single element in an empty queue
What is the printed state of the queue after enqueuing only the element 5?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Queue:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self, data):
        new_node = Node(data)
        if self.rear is None:
            self.front = self.rear = new_node
            return
        self.rear.next = new_node
        self.rear = new_node

    def print_queue(self):
        current = self.front
        while current:
            print(current.data, end=' -> ')
            current = current.next
        print('null')

q = Queue()
q.enqueue(5)
q.print_queue()
A5 -> null
B5
Cnull
DError
Attempts:
2 left
💡 Hint
Check how enqueue handles the empty queue case.
🔧 Debug
advanced
2:00remaining
Identify the error in enqueue method
What error will occur when running this enqueue method code?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Queue:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self, data):
        new_node = Node(data)
        if self.rear == None:
            self.front = new_node
        else:
            self.rear.next = new_node
        self.rear = new_node

q = Queue()
q.enqueue(1)
q.enqueue(2)
print(q.front.data, q.rear.data)
ATypeError: unsupported operand type(s)
BAttributeError: 'NoneType' object has no attribute 'next'
CPrints: 1 2
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check how the rear pointer is updated when the queue is empty.
🧠 Conceptual
advanced
1:30remaining
Why is rear pointer needed in linked list queue?
Why do we keep a rear pointer in a linked list implementation of a queue?
ATo reverse the queue easily
BTo allow enqueue operation in O(1) time by adding at the end directly
CTo allow dequeue operation in O(1) time by removing from the end
DTo keep track of the number of elements in the queue
Attempts:
2 left
💡 Hint
Think about how adding elements at the end works without rear pointer.
❓ Predict Output
expert
3:00remaining
Final queue state after mixed enqueues and dequeues
Given the following operations, what is the printed queue state at the end?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Queue:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self, data):
        new_node = Node(data)
        if self.rear is None:
            self.front = self.rear = new_node
            return
        self.rear.next = new_node
        self.rear = new_node

    def dequeue(self):
        if self.front is None:
            return None
        temp = self.front
        self.front = temp.next
        if self.front is None:
            self.rear = None
        return temp.data

    def print_queue(self):
        current = self.front
        while current:
            print(current.data, end=' -> ')
            current = current.next
        print('null')

q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.dequeue()
q.enqueue(4)
q.dequeue()
q.enqueue(5)
q.print_queue()
A3 -> 4 -> 5 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
C4 -> 5 -> null
D2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Trace each enqueue and dequeue step carefully.

Practice

(1/5)
1. What does the enqueue operation do in a linked list implementation of a queue?
easy
A. Adds a new element at the end (tail) of the linked list
B. Removes the first element from the linked list
C. Adds a new element at the beginning (head) of the linked list
D. Deletes the last element from the linked list

Solution

  1. Step 1: Understand enqueue operation in queue

    Enqueue means adding an element to the queue, which is done at the end (tail) to maintain FIFO order.
  2. Step 2: Linked list enqueue behavior

    In a linked list, adding at the tail means creating a new node and linking it as the last node.
  3. Final Answer:

    Adds a new element at the end (tail) of the linked list -> Option A
  4. Quick Check:

    Enqueue = Add at tail [OK]
Hint: Enqueue always adds at the tail in linked list queue [OK]
Common Mistakes:
  • Confusing enqueue with dequeue operation
  • Adding new element at head instead of tail
  • Thinking enqueue removes elements
2. Which of the following is the correct syntax to create a new node and enqueue it at the tail in Python linked list?
easy
A. new_node = Node(data); new_node.next = tail; tail = new_node
B. new_node = Node(data); tail.next = new_node; tail = new_node
C. new_node = Node(data); new_node.next = head; head = new_node
D. new_node = Node(data); head.next = new_node; head = new_node

Solution

  1. Step 1: Create new node with data

    We create a new node object with the given data.
  2. Step 2: Link new node at tail and update tail pointer

    Set current tail's next to new node, then update tail to new node.
  3. Final Answer:

    new_node = Node(data); tail.next = new_node; tail = new_node -> Option B
  4. Quick Check:

    New node linked at tail [OK]
Hint: Tail.next points to new node, then tail moves to new node [OK]
Common Mistakes:
  • Linking new node to head instead of tail
  • Not updating tail pointer after enqueue
  • Setting new_node.next to tail (wrong direction)
3. What will be the state of the queue after executing this code?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Queue:
    def __init__(self):
        self.head = None
        self.tail = None

    def enqueue(self, data):
        new_node = Node(data)
        if not self.tail:
            self.head = self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node

q = Queue()
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
current = q.head
result = []
while current:
    result.append(current.data)
    current = current.next
print(result)
medium
A. []
B. [30, 20, 10]
C. [10, 30, 20]
D. [10, 20, 30]

Solution

  1. Step 1: Enqueue 10, 20, 30 in order

    First enqueue sets head and tail to node with 10. Next enqueues add nodes at tail with 20 and 30.
  2. Step 2: Traverse from head to tail collecting data

    Traversal collects 10, then 20, then 30 in order.
  3. Final Answer:

    [10, 20, 30] -> Option D
  4. Quick Check:

    Queue order = insertion order [OK]
Hint: Enqueue adds at tail, so order stays insertion order [OK]
Common Mistakes:
  • Assuming enqueue adds at head reversing order
  • Not initializing head and tail properly
  • Confusing output list order
4. Find the error in this enqueue method for a linked list queue:
def enqueue(self, data):
    new_node = Node(data)
    if self.head is None:
        self.head = new_node
    else:
        self.tail.next = new_node
        self.tail = new_node
medium
A. Does not set new_node.next to None
B. Does not update head when queue is empty
C. Does not update tail when queue is empty
D. Does not link new_node to tail.next

Solution

  1. Step 1: Check initialization when queue is empty

    When head is None, head is set to new_node but tail is not updated.
  2. Step 2: Tail must also point to new_node when queue is empty

    Without updating tail, tail remains None causing errors on next enqueue.
  3. Final Answer:

    Does not update tail when queue is empty -> Option C
  4. Quick Check:

    Empty queue needs both head and tail set [OK]
Hint: Set both head and tail on empty queue enqueue [OK]
Common Mistakes:
  • Updating only head or only tail on empty queue
  • Forgetting to set tail.next on non-empty queue
  • Assuming new_node.next needs manual None assignment
5. You want to implement a queue using a linked list that supports enqueue and dequeue in O(1) time. Which of the following changes is necessary to the enqueue method to handle the edge case when the queue is empty?
hard
A. Set both head and tail to the new node when queue is empty
B. Set only tail to the new node when queue is empty
C. Set new node's next to head when queue is empty
D. Set tail.next to head when queue is empty

Solution

  1. Step 1: Understand queue empty state

    When queue is empty, both head and tail pointers are None.
  2. Step 2: Enqueue first element must update both pointers

    To maintain O(1) enqueue and dequeue, head and tail must both point to the new node.
  3. Step 3: This ensures correct linking and prevents errors

    Without setting both, dequeue or next enqueue operations may fail or cause errors.
  4. Final Answer:

    Set both head and tail to the new node when queue is empty -> Option A
  5. Quick Check:

    Empty queue enqueue sets head and tail [OK]
Hint: Empty queue needs head and tail set to new node [OK]
Common Mistakes:
  • Setting only tail or only head on empty enqueue
  • Linking new node incorrectly to head or tail
  • Not handling empty queue edge case
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