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
What is the output after inserting 4 at the end?
Consider a doubly linked list initially containing 1 <-> 2 <-> 3. We insert 4 at the end. What is the printed list?
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 append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.prev = last 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.append(1) dl.append(2) dl.append(3) dl.append(4) dl.print_list()
Attempts:
2 left
💡 Hint
Remember, append adds the new node at the end, so it should come after 3.
✗ Incorrect
The append method adds the new node at the end of the list. Starting from head (1), we traverse to the last node (3) and link the new node (4) after it. So the final list is 1 <-> 2 <-> 3 <-> 4 <-> null.
❓ Predict Output
intermediate2:00remaining
What is the output after inserting 10 at the end of an empty list?
We create an empty doubly linked list and insert 10 at the end. What is the printed list?
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 append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.prev = last 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.append(10) dl.print_list()
Attempts:
2 left
💡 Hint
When the list is empty, the new node becomes the head.
✗ Incorrect
Since the list is empty, the new node with data 10 becomes the head. The print_list method prints the single node followed by null.
🔧 Debug
advanced2:00remaining
What error occurs when inserting at end if 'prev' pointer is not set?
In the append method, if we forget to set new_node.prev = last, what error will occur when traversing backwards from the last node?
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 append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node # new_node.prev = last # This line is missing def print_reverse(self): current = self.head if current is None: print("null") return while current.next: current = current.next result = [] while current: result.append(str(current.data)) current = current.prev print(" <-> ".join(result) + " <-> null") dl = DoublyLinkedList() dl.append(1) dl.append(2) dl.append(3) dl.print_reverse()
Attempts:
2 left
💡 Hint
If prev is not set, it remains None. Accessing prev of None causes an error.
✗ Incorrect
Because new_node.prev is not set, the backward traversal tries to access prev of None, causing AttributeError.
🧠 Conceptual
advanced1:30remaining
Why is it important to update the 'prev' pointer when inserting at the end?
In a doubly linked list, what is the main reason to update the 'prev' pointer of the new node when inserting at the end?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backwards.
✗ Incorrect
The 'prev' pointer links the new node back to the previous node, enabling backward traversal through the list.
🚀 Application
expert2:30remaining
What is the list after multiple appends and one manual pointer error?
Given the code below, what is the output of print_list()? Note that after appending 5, the prev pointer of the last node is manually set to None.
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 append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.prev = last 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.append(1) dl.append(3) dl.append(5) # Manual pointer error last_node = dl.head while last_node.next: last_node = last_node.next last_node.prev = None dl.print_list()
Attempts:
2 left
💡 Hint
The print_list method uses next pointers only, so prev pointer changes do not affect forward printing.
✗ Incorrect
The print_list method traverses using next pointers only, so even if prev of last node is None, the forward list remains intact and prints all nodes.