Deque

Normal 3.5 Exclusive 15s 512 MB
Data StructuresMemory Management
Part 1

Both ends

Open

Implement 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.

C++
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_back and push_front each copy-construct one element from value. pop_back and pop_front each destroy exactly the element they remove.
  • operator[](0) is front() and operator[](size() - 1) is back(), however the elements arrived.
  • Pushes and pops at either end are amortized O(1)O(1) and operator[] is O(1)O(1). The tests run hundreds of thousands of each, and an O(n)O(n) operation runs into a per-scenario time limit.
  • Only the elements the deque holds exist: at any moment the number of live T objects the deque has constructed equals size(). 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::deque makes 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.

C++
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();             // 2

None. 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_front and pop_back on an empty deque are undefined behaviour, and so is operator[] out of range. The tests never do it.

  • Single-threaded. No iterators, no insert or erase in the middle.

  • At most 4⋅1054 \cdot 10^5 elements at once and 10610^6 operations per scenario.
Accepted 4/4
Acceptance 100%
Loading editor...
Input
both-ends
Expected output
PASS