0
0
DSA Pythonprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dequeue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Dequeue Operations
What is the printed state of the dequeue after these operations?

Operations:
1. Add 10 at rear
2. Add 20 at rear
3. Add 5 at front
4. Remove from rear
5. Add 15 at front
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None

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

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

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

    def remove_front(self):
        if self.front is None:
            return None
        data = self.front.data
        self.front = self.front.next
        if self.front is None:
            self.rear = None
        else:
            self.front.prev = None
        return data

    def remove_rear(self):
        if self.rear is None:
            return None
        data = self.rear.data
        self.rear = self.rear.prev
        if self.rear is None:
            self.front = None
        else:
            self.rear.next = None
        return data

    def display(self):
        current = self.front
        result = []
        while current:
            result.append(str(current.data))
            current = current.next
        print(" -> ".join(result) + " -> null")

dq = Dequeue()
dq.add_rear(10)
dq.add_rear(20)
dq.add_front(5)
dq.remove_rear()
dq.add_front(15)
dq.display()
A5 -> 10 -> 15 -> null
B15 -> 5 -> 10 -> null
C15 -> 10 -> 5 -> null
D10 -> 5 -> 15 -> null
Attempts:
2 left
💡 Hint
Trace each operation step-by-step, updating front and rear pointers.
🧠 Conceptual
intermediate
1:30remaining
Number of Nodes After Operations
If you start with an empty dequeue and perform these operations:
add_front(1), add_rear(2), add_front(3), remove_rear(), remove_front(), add_rear(4)
How many nodes remain in the dequeue?
A0
B1
C3
D2
Attempts:
2 left
💡 Hint
Count nodes after each operation carefully.
🔧 Debug
advanced
1:30remaining
Identify the Bug in remove_front Method
What error will occur when calling remove_front on a dequeue with only one node, given this code snippet?
DSA Python
def remove_front(self):
    if self.front is None:
        return None
    data = self.front.data
    self.front = self.front.next
    self.front.prev = None
    return data
ANo error, returns data correctly
BTypeError: unsupported operand type(s)
CAttributeError: 'NoneType' object has no attribute 'prev'
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Consider what happens when front becomes None after removal.
Predict Output
advanced
2:00remaining
Output After Mixed Add and Remove Operations
What is the printed state of the dequeue after these operations?

Operations:
add_rear(7)
add_front(3)
add_rear(9)
remove_front()
remove_rear()
add_front(1)
add_rear(11)
display()
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None

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

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

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

    def remove_front(self):
        if self.front is None:
            return None
        data = self.front.data
        self.front = self.front.next
        if self.front is None:
            self.rear = None
        else:
            self.front.prev = None
        return data

    def remove_rear(self):
        if self.rear is None:
            return None
        data = self.rear.data
        self.rear = self.rear.prev
        if self.rear is None:
            self.front = None
        else:
            self.rear.next = None
        return data

    def display(self):
        current = self.front
        result = []
        while current:
            result.append(str(current.data))
            current = current.next
        print(" -> ".join(result) + " -> null")

dq = Dequeue()
dq.add_rear(7)
dq.add_front(3)
dq.add_rear(9)
dq.remove_front()
dq.remove_rear()
dq.add_front(1)
dq.add_rear(11)
dq.display()
A1 -> 7 -> 11 -> null
B3 -> 7 -> 9 -> null
C7 -> 11 -> null
D1 -> 9 -> 11 -> null
Attempts:
2 left
💡 Hint
Track the front and rear after each operation carefully.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Dequeue Operations Using Linked List
What is the time complexity of add_front, add_rear, remove_front, and remove_rear operations in a dequeue implemented using a doubly linked list?
AAll operations are O(1)
Badd_front and add_rear are O(1), remove_front and remove_rear are O(n)
Cadd_front and remove_front are O(1), add_rear and remove_rear are O(n)
DAll operations are O(n)
Attempts:
2 left
💡 Hint
Consider how pointers are updated in a doubly linked list.