Bird
Raised Fist0
DSA Rustprogramming~10 mins

Traversal Forward and Backward in DSA Rust - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to iterate forward over the vector and print each element.

DSA Rust
let numbers = vec![1, 2, 3, 4, 5];
for num in numbers.[1]() {
    println!("{}", num);
}
Drag options to blanks, or click blank then click option'
Aiter_mut
Biter
Cinto_iter
Ddrain
Attempts:
3 left
💡 Hint
Common Mistakes
Using into_iter() consumes the vector, which is not needed here.
Using iter_mut() allows mutation, but we only want to read.
Using drain() removes elements, which is not intended.
2fill in blank
medium

Complete the code to iterate backward over the vector and print each element.

DSA Rust
let numbers = vec![10, 20, 30, 40];
for num in numbers.iter().[1]() {
    println!("{}", num);
}
Drag options to blanks, or click blank then click option'
Arev
Bnext
Cskip
Dtake
Attempts:
3 left
💡 Hint
Common Mistakes
Using next() advances forward one element only.
Using skip() skips elements but does not reverse.
Using take() limits elements but does not reverse.
3fill in blank
hard

Fix the error in the code to correctly iterate backward over a mutable vector and modify each element.

DSA Rust
let mut numbers = vec![1, 2, 3];
for num in numbers.[1]().[2]() {
    *num += 1;
}
Drag options to blanks, or click blank then click option'
Arev
Biter_mut
Cinto_iter
Ddrain
Attempts:
3 left
💡 Hint
Common Mistakes
Using iter() does not allow modification.
Using into_iter() consumes the vector and may not allow mutable references.
Using drain() removes elements.
4fill in blank
hard

Fill both blanks to create a vector of squares of numbers greater than 3, iterating forward.

DSA Rust
let numbers = vec![1, 2, 3, 4, 5];
let squares: Vec<_> = numbers.[1]()
    .filter(|&x| x [2] 3)
    .map(|x| x * x)
    .collect();
Drag options to blanks, or click blank then click option'
Aiter
B>
C<
Diter_mut
Attempts:
3 left
💡 Hint
Common Mistakes
Using iter_mut() when no mutation is needed.
Using < instead of > in the filter condition.
5fill in blank
hard

Fill all three blanks to create a reversed vector of doubled numbers less than 5.

DSA Rust
let numbers = vec![1, 2, 3, 4, 5];
let result: Vec<_> = numbers.[1]()
    .filter(|&x| x [2] 5)
    .map(|x| x * 2)
    .[3]()
    .collect();
Drag options to blanks, or click blank then click option'
Aiter
B<
Crev
Diter_mut
Attempts:
3 left
💡 Hint
Common Mistakes
Using iter_mut() when no mutation is needed.
Using > instead of < in the filter.
Forgetting to reverse with rev().

Practice

(1/5)
1. What does "Traversal Forward and Backward" mean in the context of a doubly linked list?
easy
A. Skipping nodes while moving forward
B. Moving through the list from the first node to the last and then back from the last to the first
C. Only moving from the first node to the last node
D. Moving randomly between nodes

Solution

  1. Step 1: Understand doubly linked list structure

    A doubly linked list has nodes connected both forward and backward, allowing movement in both directions.
  2. Step 2: Define traversal forward and backward

    Traversal forward means moving from the first node to the last node, and traversal backward means moving from the last node back to the first node.
  3. Final Answer:

    Moving through the list from the first node to the last and then back from the last to the first -> Option B
  4. Quick Check:

    Forward and backward traversal = Moving both ways [OK]
Hint: Think of walking to the end and then returning back [OK]
Common Mistakes:
  • Confusing traversal with skipping nodes
  • Thinking traversal is only forward
  • Assuming random movement is traversal
2. Which of the following Rust code snippets correctly traverses a doubly linked list forward using a mutable reference?
easy
A. for node in list { println!("{}", node.value); }
B. while let Some(node) = current { println!("{}", node.value); current = node.prev.as_deref_mut(); }
C. while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); }
D. loop { println!("{}", current.value); current = current.prev; }

Solution

  1. Step 1: Identify forward traversal in Rust

    Forward traversal moves from current node to next node using the next pointer.
  2. Step 2: Check code correctness

    while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); } uses current = node.next.as_deref_mut() which correctly moves forward. while let Some(node) = current { println!("{}", node.value); current = node.prev.as_deref_mut(); } moves backward, C is invalid syntax for linked list, D loops backward without condition.
  3. Final Answer:

    while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); } -> Option C
  4. Quick Check:

    Forward traversal uses next pointer [OK]
Hint: Forward traversal uses next pointer, backward uses prev [OK]
Common Mistakes:
  • Using prev pointer for forward traversal
  • Incorrect loop syntax for linked list
  • Ignoring option handling in Rust
3. Given the following Rust code for a doubly linked list traversal backward, what will be printed?
struct Node { value: i32, prev: Option, next: Option }

fn traverse_backward(mut current: Option<&Node>) {
    while let Some(node) = current {
        print!("{} ", node.value);
        current = node.prev.as_deref();
    }
}

// Assume list nodes with values 1 -> 2 -> 3, current starts at node with value 3
medium
A. 3 2 1
B. 1 2 3
C. 3 1 2
D. 1 3 2

Solution

  1. Step 1: Understand backward traversal

    Starting at node with value 3, moving backward means following prev pointers to 2, then 1.
  2. Step 2: Trace the print output

    Prints 3 first, then 2, then 1, each followed by a space.
  3. Final Answer:

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

    Backward traversal prints nodes in reverse order [OK]
Hint: Backward traversal prints nodes from tail to head [OK]
Common Mistakes:
  • Confusing forward and backward order
  • Assuming traversal starts at head
  • Ignoring Option handling in Rust
4. Identify the error in the following Rust code snippet that attempts to traverse a doubly linked list forward:
fn traverse(mut current: Option<&Node>) {
    while current.is_some() {
        println!("{}", current.unwrap().value);
        current = current.unwrap().next.as_deref();
    }
}
medium
A. The loop condition should be while current.is_none()
B. println! macro syntax is incorrect
C. next pointer should be accessed with prev instead
D. Using unwrap() multiple times causes panic if current is None

Solution

  1. Step 1: Analyze unwrap() usage

    Calling unwrap() twice without checking can panic if current becomes None between calls.
  2. Step 2: Suggest safer approach

    Use while let Some(node) = current to safely unwrap once per iteration.
  3. Final Answer:

    Using unwrap() multiple times causes panic if current is None -> Option D
  4. Quick Check:

    Multiple unwrap() calls risk panic [OK]
Hint: Use while let Some(node) = current to avoid unwrap panic [OK]
Common Mistakes:
  • Confusing is_some() and is_none()
  • Accessing wrong pointer for forward traversal
  • Misusing println! macro
5. You have a doubly linked list with nodes containing values [10, 20, 30, 40]. Write a Rust function that traverses forward to collect values into a vector, then traverses backward from the last node to collect values into another vector. What will be the content of both vectors after traversal?
hard
A. Forward: [10, 20, 30, 40], Backward: [40, 30, 20, 10]
B. Forward: [40, 30, 20, 10], Backward: [10, 20, 30, 40]
C. Forward: [10, 20, 30], Backward: [40, 30, 20]
D. Forward: [], Backward: []

Solution

  1. Step 1: Traverse forward collecting values

    Starting at head (10), collect values in order: 10, 20, 30, 40.
  2. Step 2: Traverse backward from last node collecting values

    Starting at tail (40), collect values backward: 40, 30, 20, 10.
  3. Final Answer:

    Forward: [10, 20, 30, 40], Backward: [40, 30, 20, 10] -> Option A
  4. Quick Check:

    Forward and backward vectors match traversal order [OK]
Hint: Forward collects from head to tail, backward from tail to head [OK]
Common Mistakes:
  • Mixing order of forward and backward vectors
  • Missing last node in traversal
  • Confusing vector contents with node pointers
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