Challenge - 5 Problems
Insert at Middle 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 at position 3?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> null. We insert a new node with value 25 at position 3 (1-based index). What is the resulting list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def insert_at_position(head, data, pos): new_node = Node(data) if pos == 1: new_node.next = head return new_node current = head count = 1 while current and count < pos - 1: current = current.next count += 1 if not current: return head new_node.next = current.next current.next = new_node return head def print_list(head): result = [] current = head while current: result.append(current.data) current = current.next return result head = Node(10) head.next = Node(20) head.next.next = Node(30) head.next.next.next = Node(40) head = insert_at_position(head, 25, 3) print(print_list(head))
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be the third node in the list.
✗ Incorrect
The new node with value 25 is inserted after the second node (20), so it becomes the third node. The list updates to 10 -> 20 -> 25 -> 30 -> 40 -> null.
❓ Predict Output
intermediate2:00remaining
What is the output after inserting at position 1?
Given a linked list 5 -> 15 -> 25 -> null, insert a node with value 1 at position 1. What is the resulting list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def insert_at_position(head, data, pos): new_node = Node(data) if pos == 1: new_node.next = head return new_node current = head count = 1 while current and count < pos - 1: current = current.next count += 1 if not current: return head new_node.next = current.next current.next = new_node return head def print_list(head): result = [] current = head while current: result.append(current.data) current = current.next return result head = Node(5) head.next = Node(15) head.next.next = Node(25) head = insert_at_position(head, 1, 1) print(print_list(head))
Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the new head.
✗ Incorrect
The new node with value 1 is inserted at the start, so it points to the old head (5). The list becomes 1 -> 5 -> 15 -> 25 -> null.
🔧 Debug
advanced2:30remaining
Why does this insertion code fail to insert at position 4?
The code below tries to insert a node with value 50 at position 4 in the list 10 -> 20 -> 30 -> null, but the list remains unchanged. What is the bug?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def insert_at_position(head, data, pos): new_node = Node(data) current = head count = 1 while current and count < pos: current = current.next count += 1 if not current: return head new_node.next = current.next current.next = new_node return head head = Node(10) head.next = Node(20) head.next.next = Node(30) head = insert_at_position(head, 50, 4) # Print list result = [] cur = head while cur: result.append(cur.data) cur = cur.next print(result)
Attempts:
2 left
💡 Hint
Think about where the pointer 'current' stops before insertion.
✗ Incorrect
The loop moves current to the node at position 4, but insertion requires stopping at position 3 to link the new node. Changing the loop to count < pos - 1 fixes this.
❓ Predict Output
advanced2:00remaining
What is the output after inserting at position beyond list length?
Given a linked list 1 -> 2 -> 3 -> null, what is the output after trying to insert 99 at position 5?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def insert_at_position(head, data, pos): new_node = Node(data) if pos == 1: new_node.next = head return new_node current = head count = 1 while current and count < pos - 1: current = current.next count += 1 if not current: return head new_node.next = current.next current.next = new_node return head def print_list(head): result = [] current = head while current: result.append(current.data) current = current.next return result head = Node(1) head.next = Node(2) head.next.next = Node(3) head = insert_at_position(head, 99, 5) print(print_list(head))
Attempts:
2 left
💡 Hint
If the position is beyond the list length, the list remains unchanged.
✗ Incorrect
The function returns the original list because position 5 is beyond the current list length of 3, so no insertion happens.
🧠 Conceptual
expert1:30remaining
What is the time complexity of inserting at a specific middle position in a singly linked list?
Assuming a singly linked list with n nodes, what is the time complexity to insert a new node at position k (1 <= k <= n+1)?
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit before inserting.
✗ Incorrect
To insert at position k, you must move through k-1 nodes to reach the insertion point, so the time is proportional to k, which is O(k).