Challenge - 5 Problems
Head Insert 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 nodes at the beginning?
Consider a singly linked list initially empty. We insert nodes with values 10, 20, and 30 at the beginning one by one. What is the final state of the linked list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node def __str__(self): result = [] current = self.head while current: result.append(str(current.data)) current = current.next return ' -> '.join(result) + ' -> null' ll = LinkedList() ll.insert_at_beginning(10) ll.insert_at_beginning(20) ll.insert_at_beginning(30) print(ll)
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
✗ Incorrect
Each new node is added before the current head, so the last inserted node (30) is at the front, followed by 20, then 10.
❓ Predict Output
intermediate2:00remaining
What happens if we insert None at the beginning?
Given the linked list code for inserting at the beginning, what will be the output if we insert None as data once into an empty list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node def __str__(self): result = [] current = self.head while current: result.append(str(current.data)) current = current.next return ' -> '.join(result) + ' -> null' ll = LinkedList() ll.insert_at_beginning(None) print(ll)
Attempts:
2 left
💡 Hint
The code does not restrict data values; None is a valid data.
✗ Incorrect
The node stores None as data and becomes the head. The list has one node with data None.
🔧 Debug
advanced2:30remaining
Why does this head insert code cause an infinite loop?
This code tries to insert a node at the beginning but causes an infinite loop when printing. What is the bug?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) self.head = new_node new_node.next = self.head def __str__(self): result = [] current = self.head while current: result.append(str(current.data)) current = current.next return ' -> '.join(result) + ' -> null' ll = LinkedList() ll.insert_at_beginning(5) ll.insert_at_beginning(10) print(ll)
Attempts:
2 left
💡 Hint
Check the order of assignments in insert_at_beginning method.
✗ Incorrect
Assigning head before setting new_node.next causes new_node.next to point to itself, creating an infinite loop when traversing.
🧠 Conceptual
advanced1:00remaining
What is the time complexity of inserting at the beginning of a singly linked list?
Consider inserting a new node at the beginning (head) of a singly linked list. What is the time complexity of this operation?
Attempts:
2 left
💡 Hint
Think about how many nodes you need to visit to insert at the head.
✗ Incorrect
Inserting at the beginning only changes the head pointer and the new node's next pointer, which takes constant time.
❓ Predict Output
expert2:30remaining
What is the output after multiple head inserts and one traversal?
Given the code below, what is printed after all insertions?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node def traverse_and_collect(self): result = [] current = self.head while current: result.append(current.data) current = current.next return result ll = LinkedList() for value in [1, 2, 3, 4]: ll.insert_at_beginning(value) print(ll.traverse_and_collect())
Attempts:
2 left
💡 Hint
Each new value is inserted at the front, so the last inserted is first in the list.
✗ Incorrect
Inserting at the beginning reverses the order of inserted elements. The last inserted (4) is at the head.