Appearance
Resizing Array based Queue
A fixed-size circular buffer works well --- until it fills up. In real systems, you can't always predict the size needed ahead of time.
The solution? A resizing circular buffer, which:
- Doubles the capacity when full
- Preserves queue order (head to tail)
- Avoids reallocation on every push
Your Task
Extend your array-based queue to support automatic resizing:
- Double capacity on overflow
- Maintain O(1) amortized performance
- Keep all existing queue operations working:
push,pop,front,empty
Implement a queue that resizes its circular buffer when full.
cpp
#include <vector>
template <typename T>
class ResizingArrayQueue {
private:
void resize() {
// TODO: Double the capacity and copy elements in order
}
public:
ResizingArrayQueue(int initialCapacity = 4) {
// TODO: Initialize data structures
}
void push(const T& val) {
// TODO: Push and resize if full
}
void pop() {
// TODO: Remove from front
}
T& front() {
// TODO: Return front element
}
bool empty() const {
return count == 0;
}
};