SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Implement Queue Using Stacks

Combine two stacks to preserve first-in, first-out queue order.

Try It Yourself

Use the editor below to process queue operations.

Use only stack operations

Separate newly pushed values from values ready to leave. Interleaved pushes, peeks, pops, and empty checks must preserve first-in, first-out order.

Submit your implementation when it passes the examples.

Implement Queue Using Stacks

easyQueue · Stack · Design30 minLC 232
Problem

Process queue operations using only stack-style push and pop behavior internally.

Return one output per operation. Constructor and push produce null; pop and peek return values; empty returns a boolean.

Example 1:

Input: operations = ["MyQueue", "push", "push", "peek", "pop", "empty"], values = [[], [1], [2], [], [], []]
Output: [null, null, null, 1, 1, false]
Explanation: 1 entered before 2, so peek shows 1 and pop removes it. The queue still holds 2, so empty returns false. The constructor and pushes return null.

Example 2:

Input: operations = ["MyQueue", "push", "pop", "empty"], values = [[], [7], [], []]
Output: [null, null, 7, true]
Explanation: After pushing 7, pop removes it and returns 7. The queue then has no items, so empty returns true.

Constraints

  • At most 100 operations are performed.
  • pop and peek are called only on a non-empty queue.
0 attempts

Test results

Results appear here after a run.

Solution

Transferring 3, 2, 1 from input places 1 at the top of output, ready to leave first.

Push new values onto an input stack. When a front value is needed and the output stack is empty, move every input value to the output stack.

For pushes 1, 2, the first pop transfers both values, then returns 1. Push 3 into the input stack. The output stack still has 2, so the next peek must read 2. Moving 3 now would put it ahead of an older item. Transfer again only when the output stack is empty.

function runQueueUsingStacks(operations, values) {
  class MyQueue {
    constructor() {
      this.input = [];
      this.output = [];
    }

    moveIfNeeded() {
      if (this.output.length > 0) return;

      while (this.input.length > 0) {
        this.output.push(this.input.pop());
      }
    }

    push(value) {
      this.input.push(value);
    }

    pop() {
      this.moveIfNeeded();
      return this.output.pop();
    }

    peek() {
      this.moveIfNeeded();
      return this.output[this.output.length - 1];
    }

    empty() {
      return this.input.length === 0 && this.output.length === 0;
    }
  }

  let queue;
  const output = [];

  for (let index = 0; index < operations.length; index++) {
    const operation = operations[index];

    if (operation === "MyQueue") {
      queue = new MyQueue();
      output.push(null);
    } else if (operation === "push") {
      queue.push(values[index][0]);
      output.push(null);
    } else {
      output.push(queue[operation]());
    }
  }

  return output;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Across n operations, each value transfers at most once. One pop or peek can take O(n), but the amortized cost is O(1) per operation.
Auxiliary spaceO(n)The two stacks together store all queued values.

How is this practice?

Previous lessonQueueNext lessonCircular Queues
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation