Challenge - 5 Problems
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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 state of the list after these insertions?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn insert_tail(&mut self, value: i32) {
let new_node = Box::new(Node { data: value, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next_node) = current.next {
current = next_node;
}
current.next = Some(new_node);
}
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.data);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.insert_tail(10);
list.insert_tail(20);
list.insert_tail(30);
list.print_list();
}Attempts:
2 left
💡 Hint
Think about how inserting at the tail adds nodes to the end of the list.
✗ Incorrect
Inserting at the tail means each new node is added after the last node. So the list grows as 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateWhat is the list after inserting at tail twice with same value?
Starting with an empty singly linked list, we insert the value 5 twice at the tail. What is the printed state of the list?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn insert_tail(&mut self, value: i32) {
let new_node = Box::new(Node { data: value, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next_node) = current.next {
current = next_node;
}
current.next = Some(new_node);
}
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.data);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.insert_tail(5);
list.insert_tail(5);
list.print_list();
}Attempts:
2 left
💡 Hint
Each insert at tail adds a new node at the end, even if values are the same.
✗ Incorrect
Inserting 5 twice at the tail creates two nodes with value 5 linked one after another.
🔧 Debug
advancedWhy does this tail insert code cause a runtime error?
This Rust code for inserting at tail causes a runtime error (panic). What is the cause?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn insert_tail(&mut self, value: i32) {
let new_node = Box::new(Node { data: value, next: None });
let mut current = self.head.as_mut().unwrap();
while current.next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = Some(new_node);
}
}
fn main() {
let mut list = LinkedList { head: None };
list.insert_tail(1);
}Attempts:
2 left
💡 Hint
Check what happens when the list is empty and unwrap() is called.
✗ Incorrect
When the list is empty, self.head is None. Calling unwrap() on None causes a panic at runtime.
🧠 Conceptual
advancedWhat is the time complexity of inserting at tail in a singly linked list without tail pointer?
If a singly linked list does not maintain a tail pointer, what is the time complexity of inserting a new node at the end (tail) of the list?
Attempts:
2 left
💡 Hint
Think about how to find the last node without a direct pointer.
✗ Incorrect
Without a tail pointer, we must start at the head and move through each node until the last one, which takes O(n) time.
🚀 Application
expertAfter these tail insertions, what is the list state?
Given this Rust code that inserts values 1, 2, 3, 4 at the tail of an initially empty singly linked list, what is the printed output?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn insert_tail(&mut self, value: i32) {
let new_node = Box::new(Node { data: value, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next_node) = current.next {
current = next_node;
}
current.next = Some(new_node);
}
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.data);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.insert_tail(1);
list.insert_tail(2);
list.insert_tail(3);
list.insert_tail(4);
list.print_list();
}Attempts:
2 left
💡 Hint
Inserting at tail adds nodes in the order given.
✗ Incorrect
Each insert_tail adds the new node at the end, so the list prints in insertion order.
