Challenge - 5 Problems
Tail 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 tail?
Consider a singly linked list initially empty. We insert nodes with values 10, 20, and 30 at the tail in that order. What is the printed linked list after these 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_tail(self, data): new_node = Node(data) if not self.head: self.head = new_node return temp = self.head while temp.next: temp = temp.next temp.next = new_node def print_list(self): temp = self.head while temp: print(temp.data, end=' -> ') temp = temp.next print('null') ll = LinkedList() ll.insert_at_tail(10) ll.insert_at_tail(20) ll.insert_at_tail(30) ll.print_list()
Attempts:
2 left
💡 Hint
Remember that inserting at tail adds the new node at the end of the list.
✗ Incorrect
Inserting at tail means the new node is added after the last node. So the order of nodes is the same as insertion order: 10, then 20, then 30.
❓ Predict Output
intermediate2:00remaining
What is the linked list after these tail insertions?
Given the following code inserting values 5, 15, and 25 at the tail of an empty linked list, what will be the printed output?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None def insert_tail(self, val): node = Node(val) if self.head is None: self.head = node return current = self.head while current.next: current = current.next current.next = node def display(self): curr = self.head while curr: print(curr.val, end=' -> ') curr = curr.next print('null') ll = LinkedList() ll.insert_tail(5) ll.insert_tail(15) ll.insert_tail(25) ll.display()
Attempts:
2 left
💡 Hint
Tail insertion keeps the order of insertion intact.
✗ Incorrect
Each new node is added after the last node, so the list order is 5, then 15, then 25.
🔧 Debug
advanced2:30remaining
Why does this tail insertion code cause an infinite loop?
Examine the code below that tries to insert a node at the tail of a linked list. Why does it cause an infinite loop when inserting multiple nodes?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_tail(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: temp = self.head while temp.next != new_node: temp = temp.next temp.next = new_node ll = LinkedList() ll.insert_tail(1) ll.insert_tail(2) ll.insert_tail(3)
Attempts:
2 left
💡 Hint
Check the condition inside the while loop carefully.
✗ Incorrect
The loop condition compares temp.next to new_node, which is never true initially, so the loop never ends. It should check for temp.next != None to find the last node.
❓ Predict Output
advanced2:00remaining
What is the linked list after these tail insertions with duplicates?
A linked list is empty initially. We insert values 7, 7, 14, 21 at the tail. What is the printed linked list?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None def insert_tail(self, val): new_node = Node(val) if not self.head: self.head = new_node return curr = self.head while curr.next: curr = curr.next curr.next = new_node def print_list(self): curr = self.head while curr: print(curr.val, end=' -> ') curr = curr.next print('null') ll = LinkedList() ll.insert_tail(7) ll.insert_tail(7) ll.insert_tail(14) ll.insert_tail(21) ll.print_list()
Attempts:
2 left
💡 Hint
Tail insertion preserves the order and duplicates are allowed.
✗ Incorrect
Nodes are added at the end in the order inserted, so duplicates appear in the order inserted.
🧠 Conceptual
expert1:30remaining
What is the time complexity of inserting n nodes at the tail in a singly linked list without tail pointer?
If you insert n nodes one by one at the tail of a singly linked list that does NOT maintain a tail pointer, what is the total time complexity?
Attempts:
2 left
💡 Hint
Think about how many nodes you traverse for each insertion.
✗ Incorrect
Without a tail pointer, each insertion requires traversing the whole list to find the last node, which takes O(k) time for the k-th insertion. Summing over n insertions gives O(n^2).