Challenge - 5 Problems
Doubly Linked List Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after inserting at position 2 in a doubly linked list
What is the printed state of the doubly linked list after inserting the value 15 at position 2?
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 not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.prev = last def insert_at_position(self, data, position): new_node = Node(data) if position == 1: new_node.next = self.head if self.head: self.head.prev = new_node self.head = new_node return temp = self.head for _ in range(position - 2): if temp is None: return temp = temp.next if temp is None: return new_node.next = temp.next if temp.next: temp.next.prev = new_node temp.next = new_node new_node.prev = temp def print_list(self): temp = self.head result = [] while temp: result.append(temp.data) temp = temp.next print(' -> '.join(map(str, result)) + ' -> null') dll = DoublyLinkedList() dll.append(10) dll.append(20) dll.append(30) dll.insert_at_position(15, 2) dll.print_list()
Attempts:
2 left
💡 Hint
Remember that position 1 means inserting at the head, so position 2 means after the first node.
✗ Incorrect
Inserting 15 at position 2 places it between 10 and 20. The list becomes 10 -> 15 -> 20 -> 30 -> null.
❓ Predict Output
intermediate2:00remaining
Output after inserting at head in doubly linked list
What is the printed state of the doubly linked list after inserting the value 5 at position 1?
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 not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.prev = last def insert_at_position(self, data, position): new_node = Node(data) if position == 1: new_node.next = self.head if self.head: self.head.prev = new_node self.head = new_node return temp = self.head for _ in range(position - 2): if temp is None: return temp = temp.next if temp is None: return new_node.next = temp.next if temp.next: temp.next.prev = new_node temp.next = new_node new_node.prev = temp def print_list(self): temp = self.head result = [] while temp: result.append(temp.data) temp = temp.next print(' -> '.join(map(str, result)) + ' -> null') dll = DoublyLinkedList() dll.append(10) dll.append(20) dll.append(30) dll.insert_at_position(5, 1) dll.print_list()
Attempts:
2 left
💡 Hint
Position 1 means inserting at the very start of the list.
✗ Incorrect
Inserting 5 at position 1 makes it the new head, so the list starts with 5 followed by the original nodes.
🔧 Debug
advanced2:00remaining
Identify the error in insert_at_position method
What error will occur when trying to insert at position 5 in a list of length 3 using this 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 append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.prev = last def insert_at_position(self, data, position): new_node = Node(data) if position == 1: new_node.next = self.head if self.head: self.head.prev = new_node self.head = new_node return temp = self.head for _ in range(position - 2): temp = temp.next new_node.next = temp.next if temp.next: temp.next.prev = new_node temp.next = new_node new_node.prev = temp dll = DoublyLinkedList() dll.append(10) dll.append(20) dll.append(30) dll.insert_at_position(40, 5)
Attempts:
2 left
💡 Hint
Check what happens when temp becomes None in the for loop.
✗ Incorrect
When position is beyond the list length + 1, temp becomes None and accessing temp.next causes AttributeError.
🧠 Conceptual
advanced1:30remaining
Why update prev pointer when inserting in doubly linked list?
Why is it necessary to update the prev pointer of the node after the inserted node when inserting at a specific position in a doubly linked list?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backward.
✗ Incorrect
Updating the prev pointer ensures the node after the inserted node points back correctly, preserving backward traversal.
❓ Predict Output
expert3:00remaining
Output after multiple insertions at various positions
What is the printed state of the doubly linked list after these operations?
1. Append 10
2. Append 20
3. Insert 15 at position 2
4. Insert 5 at position 1
5. Insert 25 at position 5
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 not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.prev = last def insert_at_position(self, data, position): new_node = Node(data) if position == 1: new_node.next = self.head if self.head: self.head.prev = new_node self.head = new_node return temp = self.head for _ in range(position - 2): if temp is None: return temp = temp.next if temp is None: return new_node.next = temp.next if temp.next: temp.next.prev = new_node temp.next = new_node new_node.prev = temp def print_list(self): temp = self.head result = [] while temp: result.append(temp.data) temp = temp.next print(' -> '.join(map(str, result)) + ' -> null') dll = DoublyLinkedList() dll.append(10) dll.append(20) dll.insert_at_position(15, 2) dll.insert_at_position(5, 1) dll.insert_at_position(25, 5) dll.print_list()
Attempts:
2 left
💡 Hint
Track each insertion carefully and remember positions start at 1.
✗ Incorrect
The insertions place 5 at head, 15 between 10 and 20, and 25 at the end (position 5). The final list is 5 -> 10 -> 15 -> 20 -> 25 -> null.