Skip to problem
←

Implement Stack and Queue Using a Deque

Easy 0 attempts

Implement a factory returning independent stack and queue objects, each backed by its own deque. A deque supports adding or removing at either end and inspecting either end. Implement the needed deque operations yourself; no deque object is supplied.

  • stack.push(x) adds a value; stack.pop() removes and returns the most recently added value.
  • queue.enqueue(x) adds a value; queue.dequeue() removes and returns the oldest value.
  • Each object exposes peek() to return its next removable value without consuming it, and empty() to return whether it has no values.

Insertion methods have no return value. Removals and peeks are called only on nonempty structures. Operations on one structure must not affect the other.

Function contract

Implement the factory function implementStackAndQueueUsingDeque. Fixtures contain constructor arguments and a list of [method, ...arguments] calls. Each fixture starts with a fresh instance.

Output: Return the specified result from each method. The displayed output lists results in call order; undefined results appear as null.

Example 1

Input
constructorArgs = []
operations = [["stack.push",1],["stack.push",2],["stack.pop"],["queue.enqueue",1],["queue.enqueue",2],["queue.dequeue"]]
Output
[null,null,2,null,null,1]

Example 2

Input
constructorArgs = []
operations = [["stack.empty"],["queue.empty"],["queue.enqueue",3],["queue.peek"],["stack.empty"]]
Output
[true,true,null,3,true]
Constraints
  • 1 <= number of method calls <= 100
  • 1 <= value <= 10^5
  • Removal and peek operations are called only on a nonempty structure.
Topics

Map Stack and Queue Operations to a Deque

Use an independent deque for each structure. A stack adds and removes at the back; a queue adds at the back and removes at the front. This linked deque keeps all those operations constant-time.

javascript
function implementStackAndQueueUsingDeque() {
  function deque() {
    let head = null,
      tail = null;
    return {
      addBack(value) {
        const n = { value, prev: tail, next: null };
        if (tail) tail.next = n;
        else head = n;
        tail = n;
      },
      removeBack() {
        if (!tail) return undefined;
        const v = tail.value;
        tail = tail.prev;
        if (tail) tail.next = null;
        else head = null;
        return v;
      },
      removeFront() {
        if (!head) return undefined;
        const v = head.value;
        head = head.next;
        if (head) head.prev = null;
        else tail = null;
        return v;
      },
      front() {
        return head?.value;
      },
      back() {
        return tail?.value;
      },
      empty() {
        return head === null;
      },
    };
  }
  const s = deque(),
    q = deque();
  return {
    stack: {
      push(x) {
        s.addBack(x);
      },
      pop() {
        return s.removeBack();
      },
      peek() {
        return s.back();
      },
      empty() {
        return s.empty();
      },
    },
    queue: {
      enqueue(x) {
        q.addBack(x);
      },
      dequeue() {
        return q.removeFront();
      },
      peek() {
        return q.front();
      },
      empty() {
        return q.empty();
      },
    },
  };
}

Time: O(1) per operation | Space: O(n) for stored elements

Loading discussion…

Saved in this browser only. Private to you.

JavaScript