Bird
Raised Fist0
DSA Rustprogramming~20 mins

Array Reversal Techniques in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of reversing an array slice in Rust
What is the output of this Rust code that reverses a slice of an array?
DSA Rust
fn main() {
    let arr = [1, 2, 3, 4, 5];
    let slice = &arr[1..4];
    let reversed: Vec<_> = slice.iter().rev().cloned().collect();
    println!("{:?}", reversed);
}
A[4, 3, 2]
B[5, 4, 3]
C[3, 2, 1]
D[2, 3, 4]
Attempts:
2 left
💡 Hint
Remember that slice [1..4] includes elements at indices 1, 2, and 3.
🧠 Conceptual
intermediate
1:30remaining
Understanding in-place array reversal
Which of the following best describes what happens during an in-place reversal of an array?
AElements are sorted in descending order.
BA new array is created with elements copied in reverse order.
CElements are swapped pairwise from the start and end moving towards the center without using extra space.
DElements are shifted one position to the right repeatedly.
Attempts:
2 left
💡 Hint
In-place means no extra array is created.
🔧 Debug
advanced
2:00remaining
Identify the error in this Rust array reversal code
What error does this Rust code produce when trying to reverse an array in place?
DSA Rust
fn main() {
    let arr = [1, 2, 3, 4, 5];
    let mut i = 0;
    let mut j = arr.len() - 1;
    while i < j {
        let temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        i += 1;
        j -= 1;
    }
    println!("{:?}", arr);
}
AIndex out of bounds panic at runtime
BCannot assign to `arr[i]` because `arr` is not mutable
CCompilation error: mismatched types
DNo error, prints reversed array
Attempts:
2 left
💡 Hint
Check the mutability of the array variable.
❓ Predict Output
advanced
1:30remaining
Output of reversing a vector using built-in method
What is the output of this Rust code that reverses a vector using the built-in reverse method?
DSA Rust
fn main() {
    let mut v = vec![10, 20, 30, 40];
    v.reverse();
    println!("{:?}", v);
}
A[20, 10, 40, 30]
B[10, 20, 30, 40]
C[30, 20, 10, 40]
D[40, 30, 20, 10]
Attempts:
2 left
💡 Hint
The reverse method changes the vector in place.
🚀 Application
expert
3:00remaining
Result of reversing a linked list represented as a vector of nodes
Given a singly linked list represented as a vector of nodes where each node is a tuple (value, next_index), what is the resulting list after reversing it? The list is: [(1, 1), (2, 2), (3, -1)] where -1 means end of list. Reversing means changing next_index pointers to reverse the list order.
A[(1, -1), (2, 0), (3, 1)]
B[(1, 2), (2, 1), (3, 0)]
C[(3, -1), (2, 2), (1, 1)]
D[(3, 1), (2, 0), (1, -1)]
Attempts:
2 left
💡 Hint
Reversing changes the direction of next pointers so the last node points to the previous one.

Practice

(1/5)
1. What does reversing an array mean in simple terms?
easy
A. Changing the order of elements so the last becomes first and first becomes last
B. Sorting the array in ascending order
C. Removing duplicate elements from the array
D. Adding new elements to the end of the array

Solution

  1. Step 1: Understand array reversal

    Reversing means flipping the order of elements so the last element comes first and the first element goes last.
  2. Step 2: Compare with other options

    Sorting arranges elements by value, removing duplicates deletes repeats, and adding elements is insertion, not reversal.
  3. Final Answer:

    Changing the order of elements so the last becomes first and first becomes last -> Option A
  4. Quick Check:

    Reversal = Flip order [OK]
Hint: Reversal flips order, not sorting or adding [OK]
Common Mistakes:
  • Confusing reversal with sorting
  • Thinking reversal removes elements
  • Mixing reversal with adding elements
2. Which of the following Rust code snippets correctly reverses a mutable array arr in place?
easy
A. arr = arr.reverse();
B. arr.reverse();
C. arr.reverse(&mut arr);
D. reverse(arr);

Solution

  1. Step 1: Identify correct method usage

    In Rust, the reverse() method is called directly on a mutable slice or array as arr.reverse(); to reverse it in place.
  2. Step 2: Check other options for errors

    arr = arr.reverse(); tries to assign the result of reverse() which returns (), so it's invalid. arr.reverse(&mut arr); passes &mut arr as argument, but reverse() takes no arguments. reverse(arr); calls a function reverse which is undefined.
  3. Final Answer:

    arr.reverse(); -> Option B
  4. Quick Check:

    Use method directly on mutable array [OK]
Hint: Use arr.reverse() without assignment or arguments [OK]
Common Mistakes:
  • Assigning reverse() result to array
  • Passing array as argument to reverse()
  • Calling undefined reverse function
3. What will be the output of this Rust code?
let mut arr = [1, 2, 3, 4, 5];
arr.reverse();
println!("{:?}", arr);
medium
A. [5, 2, 3, 4, 1]
B. [1, 2, 3, 4, 5]
C. [5, 4, 3, 2, 1]
D. Compilation error

Solution

  1. Step 1: Understand reverse() effect on array

    The reverse() method reverses the array elements in place, so the order becomes last to first.
  2. Step 2: Apply reverse to given array

    Original array is [1, 2, 3, 4, 5]. After reversal, it becomes [5, 4, 3, 2, 1].
  3. Final Answer:

    [5, 4, 3, 2, 1] -> Option C
  4. Quick Check:

    Reverse flips array order [OK]
Hint: Reverse flips array elements order [OK]
Common Mistakes:
  • Expecting original order after reverse
  • Thinking reverse sorts array
  • Confusing output format
4. Identify the error in this Rust code that tries to reverse an array:
let arr = [10, 20, 30, 40];
arr.reverse();
medium
A. Cannot call reverse() on an immutable array
B. reverse() method does not exist for arrays
C. Missing semicolon after array declaration
D. reverse() requires an argument

Solution

  1. Step 1: Check mutability of array

    The array arr is declared immutable by default with let. The reverse() method requires a mutable array.
  2. Step 2: Understand method requirements

    To reverse in place, the array must be mutable, declared as let mut arr. Otherwise, calling reverse() causes a compile error.
  3. Final Answer:

    Cannot call reverse() on an immutable array -> Option A
  4. Quick Check:

    reverse() needs mutable array [OK]
Hint: Make array mutable with 'mut' before reversing [OK]
Common Mistakes:
  • Forgetting to add 'mut' to array declaration
  • Thinking reverse() needs arguments
  • Assuming reverse() is not available for arrays
5. Given a vector vec = vec![1, 2, 3, 4, 5], which code snippet correctly reverses it and returns a new reversed vector without modifying the original?
hard
A. let rev_vec = vec.into_iter().reverse().collect();
B. vec.reverse(); let rev_vec = vec.clone();
C. let rev_vec = vec.reverse();
D. let rev_vec = vec.iter().rev().cloned().collect::();

Solution

  1. Step 1: Understand vector reversal without mutation

    To reverse without changing original, use iterator rev() on iter(), clone elements, then collect into new vector.
  2. Step 2: Analyze options

    let rev_vec = vec.iter().rev().cloned().collect::(); uses vec.iter().rev().cloned().collect() which creates a new reversed vector. vec.reverse(); let rev_vec = vec.clone(); reverses original vector in place, modifying it. let rev_vec = vec.reverse(); tries to assign reverse() result which is (), invalid. let rev_vec = vec.into_iter().reverse().collect(); uses into_iter() which consumes the original vector, so it doesn't leave the original unmodified.
  3. Final Answer:

    let rev_vec = vec.iter().rev().cloned().collect::(); -> Option D
  4. Quick Check:

    Use iter().rev().cloned().collect() for new reversed vector [OK]
Hint: Use iter().rev().cloned().collect() for reversed copy [OK]
Common Mistakes:
  • Modifying original vector instead of copying
  • Assigning reverse() result directly
  • Using invalid reverse() on iterator
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