Deque
Both ends
OpenImplement every member: pushes and pops at both ends, front, back, operator[], size, empty, clear and the destructor.
The tests check every index against a reference model after every operation, push hundreds of thousands of elements at the front, and run a queue for a million operations while measuring the peak memory the deque holds. They count constructions and destructions of their element type, which has to match size() at every step.
Implement Deque<T>, a double-ended queue that manages its own raw storage and mirrors the core of std::deque<T>: constant-time pushes and pops at both ends, constant-time indexing, and, in Part 2, references that survive pushes at either end.
template <class T>
class Deque {
public:
Deque() noexcept;
~Deque();
Deque(const Deque&) = delete;
Deque& operator=(const Deque&) = delete;
void push_back(const T& value);
void push_front(const T& value);
void pop_back(); // the deque is never empty here
void pop_front(); // the deque is never empty here
T& front(); const T& front() const;
T& back(); const T& back() const;
T& operator[](std::size_t i); // i counts from the front, 0 <= i < size()
const T& operator[](std::size_t i) const;
std::size_t size() const noexcept;
bool empty() const noexcept;
void clear() noexcept; // destroys every element
};push_backandpush_fronteach copy-construct one element fromvalue.pop_backandpop_fronteach destroy exactly the element they remove.operator[](0)isfront()andoperator[](size() - 1)isback(), however the elements arrived.- Pushes and pops at either end are amortized and
operator[]is . The tests run hundreds of thousands of each, and an operation runs into a per-scenario time limit. - Only the elements the deque holds exist: at any moment the number of live
Tobjects the deque has constructed equalssize(). Allocate raw storage and construct into it with placement new;new T[n]constructs objects the deque does not hold. - Storage the deque no longer needs has to go back: a queue that never holds more than a handful of elements must not keep growing however long it runs.
- Part 2 adds the guarantee
std::dequemakes about references: pushing or popping at either end never moves an element that stays in the deque, so a pointer or reference to it stays valid.
T is copy-constructible and nothrow-move-constructible, with a non-throwing destructor. The tests count every construction, copy, move and destruction of their element type and abort on a double destroy.
The harness defines the standard containers away: vector, deque, list, forward_list, array, valarray, map, multimap, set, multiset, unordered_map, unordered_multimap, unordered_set and unordered_multiset may not appear in a submission, member and variable names included. std::allocator, placement new and std::launder are fine. Do not write main; the harness owns it. The standard headers are already included.
Deque<int> d;
d.push_back(2);
d.push_back(3);
d.push_front(1); // 1 2 3
d[0]; // 1
d.back(); // 3
d.pop_front(); // 2 3
d.front(); // 2None. The judge compiles your class into a hidden harness that drives it directly and reports one row per scenario.
None. Your code prints nothing; the harness owns the verdict.
front,back,pop_frontandpop_backon an empty deque are undefined behaviour, and so isoperator[]out of range. The tests never do it.Single-threaded. No iterators, no
insertorerasein the middle.
- At most elements at once and operations per scenario.
both-ends
PASS