Bird
Raised Fist0
DSA Rustprogramming~20 mins

Insert at End of Doubly Linked List in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting 4 at the end?
Consider a doubly linked list initially containing 1 <-> 2 <-> 3. We insert 4 at the end. What is the printed list?
DSA Rust
struct Node {
    data: i32,
    prev: Option<*mut Node>,
    next: Option<Box<Node>>,
}

struct DoublyLinkedList {
    head: Option<Box<Node>>,
}

impl DoublyLinkedList {
    fn new() -> Self {
        DoublyLinkedList { head: None }
    }

    fn insert_end(&mut self, data: i32) {
        let mut new_node = Box::new(Node { data, prev: None, next: None });
        if self.head.is_none() {
            self.head = Some(new_node);
            return;
        }
        let mut current = self.head.as_mut().unwrap();
        while current.next.is_some() {
            current = current.next.as_mut().unwrap();
        }
        new_node.prev = Some(&mut **current as *mut 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 dll = DoublyLinkedList::new();
    dll.insert_end(1);
    dll.insert_end(2);
    dll.insert_end(3);
    dll.insert_end(4);
    dll.print_list();
}
A1 <-> 2 <-> 3 <-> null
B1 <-> 2 <-> 3 <-> 4 <-> null
C4 <-> 1 <-> 2 <-> 3 <-> null
D1 <-> 2 <-> 3 <-> 4
Attempts:
2 left
💡 Hint
Trace the insert_end function and how it adds the new node at the end.
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting 10 at the end of an empty list?
Start with an empty doubly linked list. Insert 10 at the end. What is the printed list?
DSA Rust
struct Node {
    data: i32,
    prev: Option<*mut Node>,
    next: Option<Box<Node>>,
}

struct DoublyLinkedList {
    head: Option<Box<Node>>,
}

impl DoublyLinkedList {
    fn new() -> Self {
        DoublyLinkedList { head: None }
    }

    fn insert_end(&mut self, data: i32) {
        let new_node = Box::new(Node { data, prev: None, next: None });
        if self.head.is_none() {
            self.head = Some(new_node);
            return;
        }
        let mut current = self.head.as_mut().unwrap();
        while current.next.is_some() {
            current = current.next.as_mut().unwrap();
        }
        new_node.prev = Some(&mut **current as *mut 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 dll = DoublyLinkedList::new();
    dll.insert_end(10);
    dll.print_list();
}
Anull
Bnull <-> 10
C10
D10 <-> null
Attempts:
2 left
💡 Hint
Check how insert_end handles empty list case.
🔧 Debug
advanced
2:00remaining
What error occurs when running this insert_end code?
This Rust code tries to insert at the end of a doubly linked list. What error does it produce?
DSA Rust
fn insert_end(&mut self, data: i32) {
    let mut new_node = Box::new(Node { data, prev: None, next: None });
    if self.head.is_none() {
        self.head = Some(new_node);
        return;
    }
    let mut current = self.head.as_mut().unwrap();
    while current.next.is_some() {
        current = current.next.as_mut().unwrap();
    }
    new_node.prev = Some(Box::new(Node { data: current.data, prev: None, next: None }));
    current.next = Some(new_node);
}
ACompile-time error: cannot move out of borrowed content
BNo error, runs correctly
CRuntime panic: index out of bounds
DCompile-time error: mismatched types
Attempts:
2 left
💡 Hint
Look at how prev is assigned with Box::new(Node...) inside insert_end.
❓ Predict Output
advanced
2:00remaining
What is the output after inserting 5 at the end twice?
Start with a doubly linked list containing 1 <-> 3. Insert 5 at the end two times. What is the printed list?
DSA Rust
struct Node {
    data: i32,
    prev: Option<*mut Node>,
    next: Option<Box<Node>>,
}

struct DoublyLinkedList {
    head: Option<Box<Node>>,
}

impl DoublyLinkedList {
    fn new() -> Self {
        DoublyLinkedList { head: None }
    }

    fn insert_end(&mut self, data: i32) {
        let new_node = Box::new(Node { data, prev: None, next: None });
        if self.head.is_none() {
            self.head = Some(new_node);
            return;
        }
        let mut current = self.head.as_mut().unwrap();
        while current.next.is_some() {
            current = current.next.as_mut().unwrap();
        }
        new_node.prev = Some(&mut **current as *mut 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 dll = DoublyLinkedList::new();
    dll.insert_end(1);
    dll.insert_end(3);
    dll.insert_end(5);
    dll.insert_end(5);
    dll.print_list();
}
A1 <-> 3 <-> 5 <-> 5 <-> null
B1 <-> 3 <-> 5 <-> null
C1 <-> 3 <-> null
D5 <-> 5 <-> 1 <-> 3 <-> null
Attempts:
2 left
💡 Hint
Check how insert_end adds nodes sequentially at the end.
🧠 Conceptual
expert
2:00remaining
Why is using Box for prev problematic in a doubly linked list in Rust?
In Rust, a doubly linked list node has fields: data, prev: Option>, next: Option>. Why is this design problematic?
ABecause prev should be a raw pointer to avoid borrow checker errors
BBecause Box<Node> cannot store integers
CBecause Box<Node> for prev creates ownership cycles leading to memory leaks
DBecause Box<Node> does not allow mutable access
Attempts:
2 left
💡 Hint
Think about ownership and cycles in Rust's memory model.

Practice

(1/5)
1. What is the main purpose of inserting a node at the end of a doubly linked list?
easy
A. To reverse the list
B. To remove the first node
C. To add a new element after the last node
D. To find the middle node

Solution

  1. Step 1: Understand the structure of a doubly linked list

    A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.
  2. Step 2: Purpose of insertion at the end

    Inserting at the end means adding a new node after the current last node, updating pointers accordingly.
  3. Final Answer:

    To add a new element after the last node -> Option C
  4. Quick Check:

    Insert at end = add after last node [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
  • Confusing insertion at end with deletion
  • Thinking insertion reverses the list
  • Assuming insertion happens at the start
2. Which Rust code snippet correctly creates a new node and inserts it at the end of a doubly linked list?
easy
A. let new_node = Node::new(value); new_node.prev = Some(head); head.next = Some(new_node);
B. let new_node = Node::new(value); head.prev = Some(new_node); new_node.next = Some(head);
C. let new_node = Node::new(value); new_node.next = Some(tail); tail.prev = Some(new_node);
D. let new_node = Node::new(value); tail.next = Some(new_node); new_node.prev = Some(tail);

Solution

  1. Step 1: Create new node with given value

    Use Node::new(value) to create a new node.
  2. Step 2: Link new node after tail

    Set tail.next to new_node and new_node.prev to tail to insert at end.
  3. Final Answer:

    let new_node = Node::new(value); tail.next = Some(new_node); new_node.prev = Some(tail); -> Option D
  4. Quick Check:

    New node after tail = let new_node = Node::new(value); tail.next = Some(new_node); new_node.prev = Some(tail); [OK]
Hint: New node's prev points to tail; tail's next points to new node [OK]
Common Mistakes:
  • Linking new node before head instead of after tail
  • Swapping next and prev pointers incorrectly
  • Not updating tail's next pointer
3. Given the Rust code below, what will be the printed list after inserting 4 at the end?
struct Node {
    value: i32,
    prev: Option,
    next: Option,
}

// Assume list initially: 1 <-> 2 <-> 3
// Insert 4 at end
// Print list from head to tail
medium
A. 1 <-> 2 <-> 3 <-> 4 <-> null
B. 4 <-> 1 <-> 2 <-> 3 <-> null
C. 1 <-> 2 <-> 4 <-> 3 <-> null
D. 1 <-> 2 <-> 3 <-> null

Solution

  1. Step 1: Understand initial list structure

    The list is 1 linked to 2, linked to 3, with 3 as tail.
  2. Step 2: Insert 4 at the end

    Adding 4 after 3 updates 3.next to 4 and 4.prev to 3.
  3. Final Answer:

    1 <-> 2 <-> 3 <-> 4 <-> null -> Option A
  4. Quick Check:

    Insert at end adds 4 after 3 [OK]
Hint: New node appears after last node in printed list [OK]
Common Mistakes:
  • Inserting new node at start instead of end
  • Not updating next pointer of last node
  • Printing list without new node
4. Identify the error in this Rust code snippet for inserting at the end of a doubly linked list:
fn insert_end(tail: &mut Option, value: i32) {
    let mut new_node = Box::new(Node::new(value));
    new_node.prev = tail.clone();
    tail = Some(new_node);
}
medium
A. tail is not updated correctly; should update tail's next pointer
B. new_node.prev should be None for end insertion
C. new_node should not be boxed
D. value should be mutable

Solution

  1. Step 1: Check pointer updates

    tail is reassigned locally, but tail's next pointer is not updated to link new_node.
  2. Step 2: Correct linking needed

    Must set tail's next to new_node and new_node.prev to tail, then update tail reference.
  3. Final Answer:

    tail is not updated correctly; should update tail's next pointer -> Option A
  4. Quick Check:

    Update tail.next to new_node [OK]
Hint: Always update tail's next pointer when inserting at end [OK]
Common Mistakes:
  • Only reassigning tail without linking nodes
  • Setting prev pointer incorrectly
  • Ignoring ownership rules in Rust
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30. You want to insert 25 at the end but also keep a pointer to the middle node for quick access. After insertion, which node should the middle pointer reference to keep it as the middle of the list?
hard
A. 25
B. 20
C. 30
D. 10

Solution

  1. Step 1: Understand list before insertion

    List is 10 <-> 20 <-> 30; middle is 20 (second of three nodes).
  2. Step 2: Insert 25 at end and find new middle

    List becomes 10 <-> 20 <-> 30 <-> 25. With 4 nodes, middle is between 20 and 30, usually the second node (20) to keep balance.
  3. Final Answer:

    20 -> Option B
  4. Quick Check:

    Middle of 4 nodes = 2nd node [OK]
Hint: Middle of even nodes is lower middle [OK]
Common Mistakes:
  • Moving middle pointer to last inserted node
  • Choosing last node as middle
  • Not updating middle pointer after insertion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard