A queue is a collection where elements are added at the back and removed from the front, following the First-In-First-Out (FIFO) order.
Click to reveal answer
beginner
How does an array-based queue keep track of elements?
It uses two indexes: front to point to the first element and rear to point to the last element in the queue within the array.
Click to reveal answer
intermediate
What happens when the queue is full in an array implementation?
When the rear index reaches the end of the array and no space is left to add new elements, the queue is full and cannot accept more elements until some are removed.
Click to reveal answer
intermediate
Why might a simple array queue implementation waste space after some dequeues?
Because the front index moves forward on dequeue, the space before front becomes unused, but the array size is fixed, so new elements cannot be added at the start without shifting or using a circular approach.
Click to reveal answer
beginner
In Rust, what is a simple way to represent a queue using an array?
Use a fixed-size array with two usize variables for front and rear indexes, and methods to enqueue (add) and dequeue (remove) elements while checking for full or empty conditions.
Click to reveal answer
In an array-based queue, which operation removes the element from the front?
ADequeue
BEnqueue
CPush
DPop
✗ Incorrect
Dequeue removes the element from the front of the queue following FIFO order.
What does the 'rear' index represent in a queue implemented with an array?
APosition of the first element
BPosition of the last element
CSize of the queue
DCapacity of the array
✗ Incorrect
The 'rear' index points to the last element in the queue.
What condition indicates that the queue is empty in an array implementation?
Afront == -1
Brear == array length
Cfront == rear + 1
Dfront > rear
✗ Incorrect
When front is greater than rear, it means no elements are in the queue.
What problem does a circular queue solve compared to a simple array queue?
AAvoids wasted space after dequeues
BFaster enqueue
CAllows duplicate elements
DRemoves need for front index
✗ Incorrect
Circular queue reuses freed space by wrapping around the array.
Which Rust type is best to track positions in an array-based queue?
AString
Bi32
Cusize
Dbool
✗ Incorrect
usize is used for indexing arrays safely in Rust.
Explain how enqueue and dequeue operations work in an array-based queue.
Think about where elements enter and leave the queue.
You got /4 concepts.
Describe the limitations of a simple array queue and how a circular queue improves it.
Consider what happens when front moves forward in a fixed array.
You got /4 concepts.
Practice
(1/5)
1. What is the main characteristic of a queue implemented using an array?
easy
A. It follows Last-In-First-Out (LIFO) order
B. It follows First-In-First-Out (FIFO) order
C. It allows random access to elements
D. It stores elements in sorted order
Solution
Step 1: Understand queue behavior
A queue adds elements at the rear and removes from the front, so the first element added is the first removed.
Step 2: Compare with options
This behavior matches FIFO, not LIFO or random access or sorting.
Final Answer:
It follows First-In-First-Out (FIFO) order -> Option B
Quick Check:
Queue = FIFO [OK]
Hint: Remember queue = first in, first out [OK]
Common Mistakes:
Confusing queue with stack (LIFO)
Thinking queue allows random access
Assuming queue sorts elements
2. Which of the following is the correct Rust syntax to declare a fixed-size array of 5 integers for queue storage?
easy
A. let queue: [i32; 5] = [0; 5];
B. let queue = Vec<i32>::new(5);
C. let queue: [i32] = [0, 0, 0, 0, 0];
D. let queue = [5]int{0};
Solution
Step 1: Identify Rust array syntax
In Rust, fixed-size arrays are declared as [type; size], and can be initialized with a repeated value like [0; 5].
Step 2: Check options
let queue: [i32; 5] = [0; 5]; matches this syntax. let queue = Vec<i32>::new(5); uses Vec but with wrong new() usage. let queue: [i32] = [0, 0, 0, 0, 0]; misses size in type. let queue = [5]int{0}; is invalid Rust syntax.
Final Answer:
let queue: [i32; 5] = [0; 5]; -> Option A
Quick Check:
Rust array syntax = [type; size] [OK]
Hint: Rust arrays use [type; size] syntax [OK]
Common Mistakes:
Using Vec::new with size argument
Missing size in array type
Using non-Rust syntax like option D
3. Consider this Rust code snippet for a queue using an array:
let mut queue = [0; 3];
let mut front = 0;
let mut rear = 0;
// Enqueue 10
queue[rear] = 10;
rear += 1;
// Enqueue 20
queue[rear] = 20;
rear += 1;
// Dequeue
front += 1;
// Enqueue 30
queue[rear] = 30;
rear += 1;
println!("{:?}", &queue);
What is the printed output?
medium
A. [10, 0, 30]
B. [0, 20, 30]
C. [20, 30, 0]
D. [10, 20, 30]
Solution
Step 1: Trace enqueue and dequeue operations
Initially queue = [0,0,0], front=0, rear=0.
Enqueue 10: queue[0]=10, rear=1
Enqueue 20: queue[1]=20, rear=2
Dequeue: front=1 (10 is logically removed but still in array)
Enqueue 30: queue[2]=30, rear=3
Step 2: Check final queue array
Array is [10, 20, 30]. Dequeue only moves front pointer, does not clear value.
Final Answer:
[10, 20, 30] -> Option D
Quick Check:
Array stores all enqueued values [OK]
Hint: Dequeue moves front pointer, array values stay [OK]
Common Mistakes:
Assuming dequeue clears array slot
Mixing front and rear indexes
Confusing array content with logical queue content
4. Given this Rust queue code snippet, what is the error?