Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting at position 2 in a doubly linked list
What is the printed state of the doubly linked list after inserting the value 99 at position 2?
DSA Rust
struct Node {
data: i32,
prev: Option<Box<Node>>,
next: Option<Box<Node>>,
}
struct DoublyLinkedList {
head: Option<Box<Node>>,
}
impl DoublyLinkedList {
fn new() -> Self {
DoublyLinkedList { head: None }
}
fn append(&mut self, data: i32) {
let new_node = Box::new(Node { data, prev: None, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next) = current.next {
current = next;
}
current.next = Some(new_node);
}
}
}
fn insert_at(&mut self, data: i32, position: usize) {
if position == 0 {
let mut new_node = Box::new(Node { data, prev: None, 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 next = node.next.take();
let mut new_node = Box::new(Node { data, prev: None, next });
node.next = Some(new_node);
break;
}
current = node.next.as_mut();
index += 1;
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
let mut result = String::new();
while let Some(node) = current {
result.push_str(&format!("{} <-> ", node.data));
current = node.next.as_ref();
}
result.push_str("null");
println!("{}", result);
}
}
fn main() {
let mut dll = DoublyLinkedList::new();
dll.append(10);
dll.append(20);
dll.append(30);
dll.insert_at(99, 2);
dll.print_list();
}Attempts:
2 left
💡 Hint
Remember that position 2 means after the second element (0-based indexing).
✗ Incorrect
Inserting at position 2 places the new node after the second node (which has value 20). So the list becomes 10 <-> 20 <-> 99 <-> 30 <-> null.
❓ Predict Output
intermediateOutput after inserting at head (position 0) in doubly linked list
What is the printed state of the doubly linked list after inserting the value 5 at position 0?
DSA Rust
struct Node {
data: i32,
prev: Option<Box<Node>>,
next: Option<Box<Node>>,
}
struct DoublyLinkedList {
head: Option<Box<Node>>,
}
impl DoublyLinkedList {
fn new() -> Self {
DoublyLinkedList { head: None }
}
fn append(&mut self, data: i32) {
let new_node = Box::new(Node { data, prev: None, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next) = current.next {
current = next;
}
current.next = Some(new_node);
}
}
}
fn insert_at(&mut self, data: i32, position: usize) {
if position == 0 {
let mut new_node = Box::new(Node { data, prev: None, 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 next = node.next.take();
let mut new_node = Box::new(Node { data, prev: None, next });
node.next = Some(new_node);
break;
}
current = node.next.as_mut();
index += 1;
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
let mut result = String::new();
while let Some(node) = current {
result.push_str(&format!("{} <-> ", node.data));
current = node.next.as_ref();
}
result.push_str("null");
println!("{}", result);
}
}
fn main() {
let mut dll = DoublyLinkedList::new();
dll.append(10);
dll.append(20);
dll.append(30);
dll.insert_at(5, 0);
dll.print_list();
}Attempts:
2 left
💡 Hint
Inserting at position 0 means adding at the start of the list.
✗ Incorrect
The new node with value 5 becomes the head, so the list is 5 <-> 10 <-> 20 <-> 30 <-> null.
🔧 Debug
advancedIdentify the error in insert_at method for doubly linked list
What error will occur when running this insert_at method if we try to insert at position 1 in a list with one element?
DSA Rust
fn insert_at(&mut self, data: i32, position: usize) {
if position == 0 {
let mut new_node = Box::new(Node { data, prev: None, next: self.head.take() });
if let Some(ref mut next_node) = new_node.next {
next_node.prev = Some(new_node.clone());
}
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 next = node.next.take();
let mut new_node = Box::new(Node { data, prev: None, next });
if let Some(ref mut next_node) = new_node.next {
next_node.prev = Some(new_node.clone());
}
node.next = Some(new_node);
break;
}
current = node.next.as_mut();
index += 1;
}
}Attempts:
2 left
💡 Hint
Check if the prev pointer of the new node is assigned properly.
✗ Incorrect
The new node's prev pointer is never set to the current node, so the backward link is broken causing incorrect doubly linked list structure.
🧠 Conceptual
advancedEffect of inserting at position greater than list length
What happens if you try to insert a node at position 10 in a doubly linked list that currently has 3 nodes?
Attempts:
2 left
💡 Hint
Consider how the insert_at method traverses the list and what happens if position is beyond length.
✗ Incorrect
The method traverses nodes but does not insert if position is beyond list length, so the list remains unchanged.
❓ Predict Output
expertFinal list after multiple insertions at various positions
Given the following operations on an empty doubly linked list, what is the final printed list?
Operations:
- insert_at(1, 0)
- insert_at(2, 1)
- insert_at(3, 1)
- insert_at(4, 3)
- insert_at(5, 10)
Assume insert_at does not insert if position is out of bounds.
DSA Rust
struct Node {
data: i32,
prev: Option<Box<Node>>,
next: Option<Box<Node>>,
}
struct DoublyLinkedList {
head: Option<Box<Node>>,
}
impl DoublyLinkedList {
fn new() -> Self {
DoublyLinkedList { head: None }
}
fn insert_at(&mut self, data: i32, position: usize) {
if position == 0 {
let mut new_node = Box::new(Node { data, prev: None, 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 next = node.next.take();
let mut new_node = Box::new(Node { data, prev: None, next });
node.next = Some(new_node);
return;
}
current = node.next.as_mut();
index += 1;
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
let mut result = String::new();
while let Some(node) = current {
result.push_str(&format!("{} <-> ", node.data));
current = node.next.as_ref();
}
result.push_str("null");
println!("{}", result);
}
}
fn main() {
let mut dll = DoublyLinkedList::new();
dll.insert_at(1, 0);
dll.insert_at(2, 1);
dll.insert_at(3, 1);
dll.insert_at(4, 3);
dll.insert_at(5, 10);
dll.print_list();
}Attempts:
2 left
💡 Hint
Trace each insertion step carefully and note that insertion at position 10 does nothing.
✗ Incorrect
Insertions happen as follows:
- insert_at(1,0): list = 1
- insert_at(2,1): list = 1 <-> 2
- insert_at(3,1): list = 1 <-> 3 <-> 2
- insert_at(4,3): list = 1 <-> 3 <-> 2 <-> 4
- insert_at(5,10): position out of bounds, no insertion
Final list: 1 <-> 3 <-> 2 <-> 4 <-> null
