Bird
Raised Fist0
DSA Rustprogramming~20 mins

Enqueue Operation in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after enqueue operations?
Consider a queue implemented using a vector in Rust. What will be the printed state of the queue after the following enqueue operations?
DSA Rust
struct Queue {
    elements: Vec<i32>,
}

impl Queue {
    fn new() -> Self {
        Queue { elements: Vec::new() }
    }

    fn enqueue(&mut self, value: i32) {
        self.elements.push(value);
    }

    fn print(&self) {
        for elem in &self.elements {
            print!("{} -> ", elem);
        }
        println!("null");
    }
}

fn main() {
    let mut q = Queue::new();
    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    q.print();
}
A30 -> 20 -> 10 -> null
B10 -> 20 -> 30 -> null
Cnull
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Remember enqueue adds elements at the end of the queue.
❓ Predict Output
intermediate
2:00remaining
What error occurs if enqueue method is missing &mut self?
In Rust, what error will this code produce if the enqueue method is defined without &mut self but tries to push to the vector?
DSA Rust
struct Queue {
    elements: Vec<i32>,
}

impl Queue {
    fn enqueue(self, value: i32) {
        self.elements.push(value);
    }
}

fn main() {
    let mut q = Queue { elements: vec![] };
    q.enqueue(5);
}
Aerror[E0382]: use of moved value `self`
Berror[E0596]: cannot borrow `self.elements` as mutable, as `self` is not declared as mutable
Cerror[E0425]: cannot find value `elements` in this scope
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Pushing to a vector requires mutable access to self.
🔧 Debug
advanced
2:00remaining
Why does this enqueue implementation cause a runtime panic?
Given this queue implementation, why does calling enqueue cause a panic at runtime?
DSA Rust
struct Queue {
    elements: Vec<i32>,
    front: usize,
}

impl Queue {
    fn new() -> Self {
        Queue { elements: Vec::new(), front: 0 }
    }

    fn enqueue(&mut self, value: i32) {
        self.elements[self.front] = value;
        self.front += 1;
    }
}

fn main() {
    let mut q = Queue::new();
    q.enqueue(1);
}
ACompile-time error due to missing lifetime annotations
BStack overflow due to infinite recursion
CNo panic, code runs and enqueues correctly
DIndex out of bounds panic because elements vector is empty and accessed by index
Attempts:
2 left
💡 Hint
Accessing vector by index requires that index to exist.
❓ Predict Output
advanced
2:00remaining
What is the queue state after these enqueue and dequeue operations?
Given this queue implementation with enqueue and dequeue, what is printed after these operations?
DSA Rust
struct Queue {
    elements: Vec<i32>,
}

impl Queue {
    fn new() -> Self {
        Queue { elements: Vec::new() }
    }

    fn enqueue(&mut self, value: i32) {
        self.elements.push(value);
    }

    fn dequeue(&mut self) -> Option<i32> {
        if self.elements.is_empty() {
            None
        } else {
            Some(self.elements.remove(0))
        }
    }

    fn print(&self) {
        for elem in &self.elements {
            print!("{} -> ", elem);
        }
        println!("null");
    }
}

fn main() {
    let mut q = Queue::new();
    q.enqueue(5);
    q.enqueue(10);
    q.enqueue(15);
    q.dequeue();
    q.enqueue(20);
    q.print();
}
A10 -> 15 -> 20 -> null
B15 -> 20 -> null
C5 -> 10 -> 15 -> 20 -> null
D5 -> 10 -> 20 -> null
Attempts:
2 left
💡 Hint
Dequeue removes the first element, enqueue adds at the end.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of enqueue operation in a queue implemented with a linked list?
Consider a queue implemented using a singly linked list with pointers to both head and tail nodes. What is the time complexity of the enqueue operation?
AO(log n) due to balanced tree structure
BO(n) because we must traverse the list to find the tail
CO(1) because we add the new node at the tail directly
DO(n^2) because of nested loops in enqueue
Attempts:
2 left
💡 Hint
Having a tail pointer allows direct access to the end.

Practice

(1/5)
1. What does the enqueue operation do in a queue data structure?
easy
A. Returns the number of elements in the queue
B. Removes an element from the front of the queue
C. Checks if the queue is empty
D. Adds an element to the end of the queue

Solution

  1. Step 1: Understand queue behavior

    A queue works like a line where new people join at the end.
  2. Step 2: Define enqueue operation

    Enqueue means adding a new element at the back of the queue.
  3. Final Answer:

    Adds an element to the end of the queue -> Option D
  4. Quick Check:

    Enqueue = Add at end [OK]
Hint: Enqueue always adds at the back of the queue [OK]
Common Mistakes:
  • Confusing enqueue with dequeue
  • Thinking enqueue removes elements
  • Mixing enqueue with checking queue size
2. Which of the following is the correct Rust syntax to enqueue an element 5 into a queue implemented as a VecDeque named queue?
easy
A. queue.push_front(5);
B. queue.enqueue(5);
C. queue.push_back(5);
D. queue.insert(5);

Solution

  1. Step 1: Identify VecDeque enqueue method

    In Rust, VecDeque uses push_back to add elements at the end.
  2. Step 2: Check options for correct method

    Only queue.push_back(5); correctly adds 5 at the back.
  3. Final Answer:

    queue.push_back(5); -> Option C
  4. Quick Check:

    VecDeque enqueue = push_back [OK]
Hint: Use push_back to enqueue in VecDeque [OK]
Common Mistakes:
  • Using push_front instead of push_back
  • Using non-existent enqueue method
  • Using insert without index
3. What will be the output of the following Rust code snippet?
use std::collections::VecDeque;

fn main() {
    let mut queue = VecDeque::new();
    queue.push_back(10);
    queue.push_back(20);
    queue.push_back(30);
    queue.push_back(40);
    println!("{:?}", queue);
}
medium
A. [10, 20, 30, 40]
B. [40, 30, 20, 10]
C. [10, 20, 30]
D. Compilation error

Solution

  1. Step 1: Trace enqueue operations

    Elements 10, 20, 30, 40 are added in order at the back of the queue.
  2. Step 2: Understand VecDeque print format

    Printing the queue shows elements in insertion order inside square brackets.
  3. Final Answer:

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

    Enqueue order preserved in VecDeque [OK]
Hint: VecDeque prints elements in insertion order [OK]
Common Mistakes:
  • Assuming push_back adds at front
  • Confusing output order
  • Expecting compilation error
4. Identify the error in the following Rust code that tries to enqueue an element into a queue:
use std::collections::VecDeque;

fn main() {
    let mut queue = VecDeque::new();
    queue.enqueue(15);
    println!("{:?}", queue);
}
medium
A. VecDeque cannot store integers directly
B. VecDeque has no method named 'enqueue'
C. println! macro syntax is incorrect
D. Missing 'mut' keyword for queue variable

Solution

  1. Step 1: Check VecDeque methods

    VecDeque does not have an 'enqueue' method; it uses 'push_back' to add elements.
  2. Step 2: Verify other code parts

    The queue is mutable and println! syntax is correct; VecDeque supports integers.
  3. Final Answer:

    VecDeque has no method named 'enqueue' -> Option B
  4. Quick Check:

    Use push_back, not enqueue [OK]
Hint: VecDeque uses push_back, not enqueue [OK]
Common Mistakes:
  • Using enqueue method on VecDeque
  • Forgetting mut keyword (not error here)
  • Misreading println! syntax
5. You want to implement a queue using a Rust Vec where enqueue adds elements at the end and dequeue removes from the front. Which of the following code snippets correctly enqueues the element 7?
hard
A. queue.push(7);
B. queue.insert(0, 7);
C. queue.remove(0); queue.push(7);
D. queue.pop(); queue.push(7);

Solution

  1. Step 1: Understand enqueue in Vec-based queue

    Enqueue means adding at the end, so push adds element at the back.
  2. Step 2: Analyze options

    queue.push(7); uses push(7) which correctly adds 7 at the end. Others either insert at front or remove elements.
  3. Final Answer:

    queue.push(7); -> Option A
  4. Quick Check:

    Vec enqueue = push at end [OK]
Hint: Use push to enqueue in Vec-based queue [OK]
Common Mistakes:
  • Using insert(0, val) which adds at front
  • Removing elements before enqueue
  • Using pop which removes last element
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