Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a queue struct with a fixed-size array.
DSA Rust
struct Queue {
data: [i32; [1]],
front: usize,
rear: usize,
size: usize,
}Attempts:
3 left
💡 Hint
Common Mistakes
Using zero as array size causes compile error.
Using too large size makes it hard to visualize.
✗ Incorrect
The queue is declared with a fixed size of 5 elements in the array.
2fill in blank
mediumComplete the enqueue method to add an element to the queue.
DSA Rust
impl Queue {
fn enqueue(&mut self, value: i32) -> bool {
if self.size == self.data.len() {
return false; // Queue full
}
self.data[self.rear] = value;
self.rear = (self.rear [1] 1) % self.data.len();
self.size += 1;
true
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction causes rear to move backward incorrectly.
Using multiplication or division is not suitable for index increment.
✗ Incorrect
We increment rear by 1 and wrap around using modulo to maintain circular queue.
3fill in blank
hardFix the error in dequeue method to correctly remove and return the front element.
DSA Rust
impl Queue {
fn dequeue(&mut self) -> Option<i32> {
if self.size == 0 {
return None; // Queue empty
}
let value = self.data[self.front];
self.front = (self.front [1] 1) % self.data.len();
self.size -= 1;
Some(value)
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction causes front to move backward incorrectly.
Using multiplication or division is not suitable for index increment.
✗ Incorrect
We increment front by 1 and wrap around using modulo to maintain circular queue.
4fill in blank
hardFill both blanks to implement a method that checks if the queue is empty or full.
DSA Rust
impl Queue {
fn is_empty(&self) -> bool {
self.size [1] 0
}
fn is_full(&self) -> bool {
self.size [2] self.data.len()
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong empty/full detection.
Using '<' or '>' causes incorrect boolean logic.
✗ Incorrect
Queue is empty if size equals 0, and full if size equals array length.
5fill in blank
hardFill all three blanks to implement a method that returns a vector of current queue elements in order.
DSA Rust
impl Queue {
fn to_vec(&self) -> Vec<i32> {
let mut result = Vec::new();
for i in 0..self.size {
let index = (self.front [1] i) % self.data.len();
result.push(self.data[index [2] [3]]);
}
result
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction causes wrong indexing.
Using non-zero offset or wrong type causes compile errors.
✗ Incorrect
We add i to front and modulo array length to get index; push data[index + 0] (which is data[index]) to result vector.
