Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after inserting nodes at the beginning
What is the printed state of the doubly linked list after inserting 10, then 20, then 30 at the beginning?
DSA Python
class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class DoublyLinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head if self.head is not None: self.head.prev = new_node self.head = new_node def print_list(self): current = self.head result = [] while current: result.append(str(current.data)) current = current.next print(' -> '.join(result) + ' -> null') dl = DoublyLinkedList() dl.insert_at_beginning(10) dl.insert_at_beginning(20) dl.insert_at_beginning(30) dl.print_list()
Attempts:
2 left
💡 Hint
Remember that inserting at the beginning means the newest node becomes the head.
✗ Incorrect
Each new node is added before the current head, so the list order is reversed from insertion order.
🧠 Conceptual
intermediate1:30remaining
Understanding pointer updates in insertion at beginning
When inserting a new node at the beginning of a doubly linked list, which pointers must be updated to maintain list integrity?
Attempts:
2 left
💡 Hint
Think about how the new node connects forward and backward to the existing list.
✗ Incorrect
The new node's next must point to the old head, and the old head's prev must point back to the new node. Then the head pointer is updated.
🔧 Debug
advanced2:00remaining
Identify the error in this insertion code
What error will this code cause when inserting at the beginning of a doubly linked list?
DSA Python
def insert_at_beginning(self, data): new_node = Node(data) new_node.prev = self.head self.head = new_node if new_node.next is not None: new_node.next.prev = new_node
Attempts:
2 left
💡 Hint
Check what prev pointer of the new head node should be.
✗ Incorrect
The new node inserted at beginning should have prev = None, but code sets prev = self.head (old head), which is incorrect.
❓ Predict Output
advanced2:00remaining
Output after multiple insertions and one traversal
What is the output of the following code?
DSA Python
class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class DoublyLinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head if self.head: self.head.prev = new_node self.head = new_node def print_reverse(self): current = self.head while current and current.next: current = current.next result = [] while current: result.append(str(current.data)) current = current.prev print(' <- '.join(result) + ' <- null') dl = DoublyLinkedList() dl.insert_at_beginning(1) dl.insert_at_beginning(2) dl.insert_at_beginning(3) dl.print_reverse()
Attempts:
2 left
💡 Hint
The print_reverse method prints from tail to head.
✗ Incorrect
After inserting 3,2,1 at beginning, list is 3->2->1. Reverse traversal prints 1 <- 2 <- 3 <- null.
🧠 Conceptual
expert1:30remaining
Why is updating the previous pointer important in doubly linked list insertion?
What problem arises if you insert a node at the beginning of a doubly linked list but forget to update the old head's prev pointer to the new node?
Attempts:
2 left
💡 Hint
Think about how prev pointers help in moving backward through the list.
✗ Incorrect
If old head's prev pointer is not updated, backward traversal starting from old head or tail will miss the new node, breaking the list's integrity.