Challenge - 5 Problems
Head 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 beginning?
Consider the Rust code below that inserts nodes at the beginning of a singly linked list. What will be the printed list after all 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_at_beginning(&mut self, data: i32) {
let new_node = Box::new(Node {
data,
next: self.head.take(),
});
self.head = Some(new_node);
}
fn print_list(&self) {
let mut current = &self.head;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.insert_at_beginning(10);
list.insert_at_beginning(20);
list.insert_at_beginning(30);
list.print_list();
}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 list order is reversed from insertion order.
❓ Predict Output
intermediateWhat is the head node's data after these insertions?
Given the Rust linked list code that inserts nodes at the beginning, what is the data value of the head node after inserting 5, then 15, then 25?
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_at_beginning(&mut self, data: i32) {
let new_node = Box::new(Node {
data,
next: self.head.take(),
});
self.head = Some(new_node);
}
}
fn main() {
let mut list = LinkedList::new();
list.insert_at_beginning(5);
list.insert_at_beginning(15);
list.insert_at_beginning(25);
// What is list.head.data?
}Attempts:
2 left
💡 Hint
The last inserted node becomes the head.
✗ Incorrect
Each insertion puts the new node at the front, so the last inserted node with data 25 is the head.
🔧 Debug
advancedWhy does this Rust code fail to compile when inserting at beginning?
This Rust code tries to insert a node at the beginning of a singly linked list but fails to compile. What is the cause?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn insert_at_beginning(&mut self, data: i32) {
let new_node = Box::new(Node {
data,
next: self.head,
});
self.head = Some(new_node);
}
}
fn main() {
let mut list = LinkedList { head: None };
list.insert_at_beginning(1);
}Attempts:
2 left
💡 Hint
Check how self.head is used inside insert_at_beginning.
✗ Incorrect
Assigning next: self.head moves ownership of head, so self.head cannot be used again without take().
🧠 Conceptual
advancedWhat is the time complexity of inserting at the beginning of a singly linked list?
When inserting a new node at the beginning (head) of a singly linked list, what is the time complexity in terms of number of nodes n?
Attempts:
2 left
💡 Hint
Inserting at head does not require traversal.
✗ Incorrect
Insertion at the beginning only changes head pointer and new node's next, so it takes constant time.
🚀 Application
expertAfter inserting 1, 2, 3 at beginning, what is the list order?
You insert nodes with values 1, then 2, then 3 at the beginning of an empty singly linked list. What is the final order of nodes when printed from head to tail?
Attempts:
2 left
💡 Hint
Each new node becomes the new head.
✗ Incorrect
Inserting at beginning reverses insertion order, so last inserted node is first.
