Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Implement Stack Using Queue in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack-Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after these stack operations?
Consider a stack implemented using a single queue. The stack supports push and pop operations. What will be the printed stack state after the following operations?

push(1), push(2), push(3), pop(), push(4)
DSA Javascript
class StackUsingQueue {
  constructor() {
    this.queue = [];
  }

  push(x) {
    this.queue.push(x);
    let size = this.queue.length;
    while (size > 1) {
      this.queue.push(this.queue.shift());
      size--;
    }
  }

  pop() {
    return this.queue.shift();
  }

  print() {
    console.log(this.queue.join(' -> ') + ' -> null');
  }
}

const stack = new StackUsingQueue();
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop();
stack.push(4);
stack.print();
A1 -> 2 -> 4 -> null
B2 -> 1 -> 4 -> null
C4 -> 2 -> 1 -> null
D3 -> 2 -> 1 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that push rotates the queue to keep the last pushed element at the front.
🧠 Conceptual
intermediate
1:30remaining
Why does the push operation rotate the queue in stack implementation?
In a stack implemented using a single queue, the push operation rotates the queue after adding the new element. Why is this rotation necessary?
ATo ensure the last pushed element is always at the front for pop operation
BTo maintain the queue in ascending order
CTo sort the queue elements before popping
DTo remove duplicates from the queue
Attempts:
2 left
💡 Hint
Think about how a stack pops the last pushed element.
❓ Predict Output
advanced
2:00remaining
What error occurs in this stack pop implementation?
Given this stack implemented with two queues, what error or output occurs when pop is called on an empty stack?
DSA Javascript
class StackUsingTwoQueues {
  constructor() {
    this.q1 = [];
    this.q2 = [];
  }

  push(x) {
    this.q1.push(x);
  }

  pop() {
    if (this.q1.length === 0) {
      return this.q1.shift();
    }
    while (this.q1.length > 1) {
      this.q2.push(this.q1.shift());
    }
    const popped = this.q1.shift();
    [this.q1, this.q2] = [this.q2, this.q1];
    return popped;
  }
}

const stack = new StackUsingTwoQueues();
console.log(stack.pop());
ATypeError: Cannot read property 'shift' of undefined
Bundefined
Cnull
DReferenceError: q1 is not defined
Attempts:
2 left
💡 Hint
What happens when shift is called on an empty array?
❓ Predict Output
advanced
2:00remaining
What is the final queue state after these operations?
Using two queues to implement a stack, what is the final state of q1 after these operations?

push(10), push(20), pop(), push(30), pop()
DSA Javascript
class StackUsingTwoQueues {
  constructor() {
    this.q1 = [];
    this.q2 = [];
  }

  push(x) {
    this.q1.push(x);
  }

  pop() {
    while (this.q1.length > 1) {
      this.q2.push(this.q1.shift());
    }
    const popped = this.q1.shift();
    [this.q1, this.q2] = [this.q2, this.q1];
    return popped;
  }
}

const stack = new StackUsingTwoQueues();
stack.push(10);
stack.push(20);
stack.pop();
stack.push(30);
stack.pop();
console.log(stack.q1);
A[10]
B[30]
C[]
D[20, 30]
Attempts:
2 left
💡 Hint
Trace each operation carefully and watch how queues swap.
🧠 Conceptual
expert
1:30remaining
What is the time complexity of push and pop in stack using single queue?
In a stack implemented using a single queue where push rotates the queue, what are the time complexities of push and pop operations respectively?
Apush: O(1), pop: O(1)
Bpush: O(1), pop: O(n)
Cpush: O(n), pop: O(n)
Dpush: O(n), pop: O(1)
Attempts:
2 left
💡 Hint
Consider how many elements are moved during push and pop.

Practice

(1/5)
1. What is the main idea behind implementing a stack using a queue?
easy
A. Use two stacks to implement a queue
B. Use an array to implement a queue
C. Use a linked list to implement a stack
D. Use a queue to simulate stack operations by reversing order

Solution

  1. Step 1: Understand stack and queue behavior

    A stack follows Last-In-First-Out (LIFO), while a queue follows First-In-First-Out (FIFO).
  2. Step 2: Simulate stack using queue

    To implement stack using queue, we reverse the order of elements in the queue to mimic LIFO behavior.
  3. Final Answer:

    Use a queue to simulate stack operations by reversing order -> Option D
  4. Quick Check:

    Stack via queue = reverse order [OK]
Hint: Remember stack is LIFO, queue is FIFO; reverse queue to mimic stack [OK]
Common Mistakes:
  • Confusing stack with queue behavior
  • Trying to implement queue using stack instead
  • Ignoring order reversal needed for stack simulation
2. Which of the following is the correct syntax to enqueue an element `x` into a JavaScript queue implemented as an array named `queue`?
easy
A. queue.shift(x);
B. queue.push(x);
C. queue.pop(x);
D. queue.unshift(x);

Solution

  1. Step 1: Identify enqueue operation in JavaScript array

    Enqueue means adding element at the end of the queue, which is done by push().
  2. Step 2: Check other methods

    pop() removes from end, shift() removes from front, unshift() adds at front, so only push() adds at end.
  3. Final Answer:

    queue.push(x); -> Option B
  4. Quick Check:

    Enqueue = push() [OK]
Hint: Use push() to add at queue end in JavaScript arrays [OK]
Common Mistakes:
  • Using pop() instead of push() to add elements
  • Using shift() or unshift() incorrectly for enqueue
  • Confusing enqueue with dequeue operations
3. Consider this JavaScript code implementing a stack using one queue:
class Stack {
  constructor() {
    this.queue = [];
  }
  push(x) {
    this.queue.push(x);
    for (let i = 0; i < this.queue.length - 1; i++) {
      this.queue.push(this.queue.shift());
    }
  }
  pop() {
    return this.queue.shift();
  }
}
const s = new Stack();
s.push(1);
s.push(2);
s.push(3);
console.log(s.pop());
What will be printed?
medium
A. 2
B. 1
C. 3
D. undefined

Solution

  1. Step 1: Understand push operation

    Push adds element at end, then rotates queue to move new element to front, simulating stack top.
  2. Step 2: Trace push and pop calls

    Push 1: queue=[1]; Push 2: queue=[2,1]; Push 3: queue=[3,2,1]; Pop removes front, which is 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Stack pop = last pushed = 3 [OK]
Hint: Rotate queue after push to keep newest element front [OK]
Common Mistakes:
  • Assuming pop removes last element of array instead of front
  • Not rotating queue after push
  • Confusing queue shift() with stack pop()
4. The following code attempts to implement a stack using two queues. What is the error?
class Stack {
  constructor() {
    this.q1 = [];
    this.q2 = [];
  }
  push(x) {
    this.q1.push(x);
  }
  pop() {
    while (this.q1.length > 1) {
      this.q2.push(this.q1.shift());
    }
    return this.q1.shift();
  }
}
medium
A. pop() does not swap q1 and q2 after operation
B. push() should use q2 instead of q1
C. pop() should return q2.shift() instead of q1.shift()
D. Constructor should initialize q1 and q2 as objects, not arrays

Solution

  1. Step 1: Analyze pop method

    Pop moves all but last element from q1 to q2, then removes last from q1.
  2. Step 2: Identify missing step

    After pop, q1 is empty and q2 has elements, but q1 and q2 are not swapped, so next pop fails.
  3. Final Answer:

    pop() does not swap q1 and q2 after operation -> Option A
  4. Quick Check:

    Swap queues after pop to maintain stack [OK]
Hint: Always swap queues after pop in two-queue stack [OK]
Common Mistakes:
  • Forgetting to swap queues after pop
  • Returning from wrong queue
  • Incorrect initialization of queues
5. You want to implement a stack using a single queue in JavaScript. Which approach correctly maintains stack order after each push operation?
class Stack {
  constructor() {
    this.queue = [];
  }
  push(x) {
    this.queue.push(x);
    // What should be done here?
  }
  pop() {
    return this.queue.shift();
  }
}
hard
A. Rotate the queue by dequeuing and enqueuing all elements except the last pushed
B. Sort the queue in descending order after each push
C. Clear the queue and add elements again in reverse order
D. Do nothing; queue order naturally matches stack order

Solution

  1. Step 1: Understand stack order with one queue

    Stack is LIFO, but queue is FIFO, so after push, rotate queue to bring new element front.
  2. Step 2: Rotate queue after push

    Remove elements from front and add them to back except the last pushed, so last pushed is at front.
  3. Final Answer:

    Rotate the queue by dequeuing and enqueuing all elements except the last pushed -> Option A
  4. Quick Check:

    Rotate queue after push to simulate stack [OK]
Hint: Rotate queue after push to keep newest element at front [OK]
Common Mistakes:
  • Not rotating queue after push
  • Trying to sort queue which is inefficient
  • Assuming queue order matches stack without changes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard