Challenge - 5 Problems
Master of Insert at Middle Position
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting at middle position in a linked list
What is the printed state of the linked list after inserting value 99 at position 2?
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 append(&mut self, data: i32) {
let new_node = Box::new(Node { data, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn insert_at(&mut self, data: i32, position: usize) {
if position == 0 {
let new_node = Box::new(Node {
data,
next: self.head.take(),
});
self.head = Some(new_node);
return;
}
let mut current = self.head.as_mut();
let mut index = 0;
while let Some(node) = current {
if index + 1 == position {
let new_node = Box::new(Node {
data,
next: node.next.take(),
});
node.next = Some(new_node);
return;
}
current = node.next.as_mut();
index += 1;
}
}
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.append(10);
list.append(20);
list.append(30);
list.insert_at(99, 2);
list.print_list();
}Attempts:
2 left
💡 Hint
Remember that position 2 means after the second element (0-based indexing).
✗ Incorrect
The insert_at function inserts the new node after the node at position 1 (0-based). So the new node with value 99 is placed between 20 and 30.
❓ Predict Output
intermediateResult of inserting at position beyond list length
What is the printed state of the linked list after inserting value 50 at position 5 in a list of length 3?
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 append(&mut self, data: i32) {
let new_node = Box::new(Node { data, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn insert_at(&mut self, data: i32, position: usize) {
if position == 0 {
let new_node = Box::new(Node {
data,
next: self.head.take(),
});
self.head = Some(new_node);
return;
}
let mut current = self.head.as_mut();
let mut index = 0;
while let Some(node) = current {
if index + 1 == position {
let new_node = Box::new(Node {
data,
next: node.next.take(),
});
node.next = Some(new_node);
return;
}
current = node.next.as_mut();
index += 1;
}
// If position is beyond length, append at end
self.append(data);
}
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.append(1);
list.append(2);
list.append(3);
list.insert_at(50, 5);
list.print_list();
}Attempts:
2 left
💡 Hint
If the position is beyond the list length, the new node is appended at the end.
✗ Incorrect
Since position 5 is beyond the current length 3, the insert_at function appends the new node with value 50 at the end.
🔧 Debug
advancedIdentify the error in insert_at function
What error will this Rust code produce when trying to insert at position 1 in an empty list?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn insert_at(&mut self, data: i32, position: usize) {
let mut current = self.head.as_mut();
let mut index = 0;
while let Some(node) = current {
if index + 1 == position {
let new_node = Box::new(Node {
data,
next: node.next.take(),
});
node.next = Some(new_node);
return;
}
current = node.next.as_mut();
index += 1;
}
}
}
fn main() {
let mut list = LinkedList { head: None };
list.insert_at(10, 1);
}Attempts:
2 left
💡 Hint
Check what happens when the list is empty and position is not zero.
✗ Incorrect
The insert_at function does nothing if the list is empty and position is not zero, so no insertion happens and the list remains empty.
❓ Predict Output
advancedOutput after multiple insertions at middle positions
What is the printed state of the linked list after these operations?
- Append 5
- Append 15
- Insert 10 at position 1
- Insert 20 at position 3
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 append(&mut self, data: i32) {
let new_node = Box::new(Node { data, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn insert_at(&mut self, data: i32, position: usize) {
if position == 0 {
let new_node = Box::new(Node {
data,
next: self.head.take(),
});
self.head = Some(new_node);
return;
}
let mut current = self.head.as_mut();
let mut index = 0;
while let Some(node) = current {
if index + 1 == position {
let new_node = Box::new(Node {
data,
next: node.next.take(),
});
node.next = Some(new_node);
return;
}
current = node.next.as_mut();
index += 1;
}
self.append(data);
}
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.append(5);
list.append(15);
list.insert_at(10, 1);
list.insert_at(20, 3);
list.print_list();
}Attempts:
2 left
💡 Hint
Insertions at position 1 and 3 place nodes between existing nodes.
✗ Incorrect
Insert 10 at position 1 places it between 5 and 15. Insert 20 at position 3 places it after 15. Final list: 5 -> 10 -> 15 -> 20 -> null.
❓ Predict Output
expertOutput after inserting at middle in a doubly linked list
Given this doubly linked list code, what is the printed forward traversal after inserting 25 at position 2?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { data: i32, prev: Option<Rc<RefCell<Node>>>, next: Option<Rc<RefCell<Node>>>, } struct DoublyLinkedList { head: Option<Rc<RefCell<Node>>>, } impl DoublyLinkedList { fn new() -> Self { DoublyLinkedList { head: None } } fn append(&mut self, data: i32) { let new_node = Rc::new(RefCell::new(Node { data, prev: None, next: None })); match self.head.clone() { None => self.head = Some(new_node), Some(head) => { let mut tail = head.clone(); while let Some(next) = tail.borrow().next.clone() { tail = next; } new_node.borrow_mut().prev = Some(tail.clone()); tail.borrow_mut().next = Some(new_node); } } } fn insert_at(&mut self, data: i32, position: usize) { if position == 0 { let new_node = Rc::new(RefCell::new(Node { data, prev: None, next: self.head.clone() })); if let Some(old_head) = self.head.take() { old_head.borrow_mut().prev = Some(new_node.clone()); self.head = Some(new_node); } else { self.head = Some(new_node); } return; } let mut current = self.head.clone(); let mut index = 0; while let Some(node) = current.clone() { if index == position { let prev_node = node.borrow().prev.clone(); let new_node = Rc::new(RefCell::new(Node { data, prev: prev_node.clone(), next: Some(node.clone()) })); if let Some(prev) = prev_node { prev.borrow_mut().next = Some(new_node.clone()); } node.borrow_mut().prev = Some(new_node); if position == 0 { self.head = Some(new_node); } return; } current = node.borrow().next.clone(); index += 1; } // If position is beyond length, append self.append(data); } fn print_forward(&self) { let mut current = self.head.clone(); while let Some(node) = current { print!("{} -> ", node.borrow().data); current = node.borrow().next.clone(); } println!("null"); } } fn main() { let mut list = DoublyLinkedList::new(); list.append(10); list.append(20); list.append(30); list.insert_at(25, 2); list.print_forward(); }
Attempts:
2 left
💡 Hint
Insertion at position 2 places the new node between nodes at positions 1 and 2.
✗ Incorrect
The insert_at function inserts 25 between 20 (pos 1) and 30 (pos 2). The list becomes 10 -> 20 -> 25 -> 30 -> null.
