📖 Scenario: Imagine you are managing a line of customers waiting for service. You want to keep track of who comes first and who goes next. A queue is perfect for this because it works like a real line: first in, first out.But you only have two stacks, which work like piles of plates (last in, first out). Your task is to use these two stacks to build a queue that behaves correctly.
🎯 Goal: Build a queue using two stacks in TypeScript. You will create methods to add customers to the line and serve them in the correct order.
📋 What You'll Learn
Create two stacks as arrays named stackIn and stackOut
Create a method enqueue to add an element to the queue using stackIn
Create a method dequeue to remove and return the first element added using both stacks
Print the queue state after operations to verify correct order
💡 Why This Matters
🌍 Real World
Queues are used in many real-world systems like customer service lines, task scheduling, and print job management. Sometimes only stacks are available, so this technique helps build queues from stacks.
💼 Career
Understanding how to implement one data structure using others shows problem-solving skills and deep knowledge of data structures, useful for software engineering interviews and system design.
Progress0 / 4 steps
1
Create two empty stacks
Create two empty arrays called stackIn and stackOut to represent the two stacks.
DSA Typescript
Hint
Use [] to create empty arrays for stackIn and stackOut.
2
Create the enqueue method
Create a function called enqueue that takes a number value and adds it to the stackIn array using the push method.
DSA Typescript
Hint
Use stackIn.push(value) inside the enqueue function to add the value.
3
Create the dequeue method
Create a function called dequeue that returns a number. If stackOut is empty, move all elements from stackIn to stackOut by popping from stackIn and pushing to stackOut. Then pop and return the top element from stackOut.
DSA Typescript
Hint
Check if stackOut is empty. If yes, move all items from stackIn to stackOut. Then pop from stackOut.
4
Test the queue and print the result
Use enqueue to add 10, 20, and 30 to the queue. Then call dequeue twice. Finally, print the current state of stackIn and stackOut arrays to show the queue's internal stacks.
DSA Typescript
Hint
After dequeuing twice, stackIn should be empty and stackOut should have the last element 30.
Practice
(1/5)
1. What is the main idea behind implementing a Queue Using Two Stacks?
easy
A. Use two stacks to perform only enqueue operations
B. Use two stacks to store elements in sorted order
C. Use one stack for enqueue and another for dequeue to reverse order
D. Use two stacks to perform only dequeue operations
Solution
Step 1: Understand queue behavior
A queue follows FIFO (First In First Out) order, meaning elements are removed in the order they were added.
Step 2: Use two stacks to reverse order
One stack is used to push new elements (enqueue), and the other stack is used to pop elements (dequeue). This reverses the order twice, preserving FIFO.
Final Answer:
Use one stack for enqueue and another for dequeue to reverse order -> Option C
Quick Check:
Two stacks reverse order to simulate queue = A [OK]
Hint: Two stacks reverse order to simulate queue [OK]
Common Mistakes:
Thinking both stacks store elements in sorted order
Using stacks only for enqueue or only for dequeue
Confusing queue with stack behavior
2. Which of the following is the correct TypeScript method signature to enqueue an element in a queue implemented using two stacks?
easy
A. enqueue(element: number): void
B. enqueue(): number
C. enqueue(element: string): string
D. enqueue(element: number): number
Solution
Step 1: Understand enqueue method purpose
Enqueue adds an element to the queue and does not return anything, so return type is void.
Step 2: Check parameter and return types
Parameter should be the element to add, here a number. So signature is enqueue(element: number): void.
Final Answer:
enqueue(element: number): void -> Option A
Quick Check:
Enqueue adds element, returns nothing = D [OK]
Hint: Enqueue adds element, returns void [OK]
Common Mistakes:
Returning the element instead of void
Using wrong parameter type like string
Missing parameter in method signature
3. What will be the output after the following operations on a queue implemented using two stacks?
After all operations, the queue is empty because all enqueued elements have been dequeued. So the output after these operations is an empty queue []. However, since the options show arrays, the closest interpretation is the queue state after the last dequeue, which is empty. None of the options show empty array, so the question likely asks for the elements remaining in the queue after the operations. The last dequeue removes 30, so queue is empty. If the question means the output of the dequeue operations, they are 10, 20, 30 in order.
Final Answer:
[20, 30] -> Option B
Quick Check:
After first dequeue, queue has [20]; after enqueue(30), queue has [20,30]; after second dequeue, queue has [30]; after third dequeue, queue empty [OK]
Hint: Dequeue removes oldest element first [OK]
Common Mistakes:
Assuming dequeue removes last added element
Confusing stack pop with queue dequeue
Not tracking order of elements carefully
4. Identify the bug in this dequeue method of a queue using two stacks:
dequeue(): number | undefined { if (this.stack2.length === 0) { while (this.stack1.length > 0) { this.stack2.push(this.stack1.pop()); } } return this.stack1.pop(); }
medium
A. Pushes elements back to stack1 instead of stack2
B. Does not check if both stacks are empty
C. Uses pop() instead of shift() on stack
D. Returns from stack1 instead of stack2 after transfer
Solution
Step 1: Understand dequeue logic
If stack2 is empty, elements from stack1 are moved to stack2 to reverse order. Then dequeue should pop from stack2.
Step 2: Identify incorrect return statement
Code returns this.stack1.pop() instead of this.stack2.pop(), so it returns wrong element or undefined.
Final Answer:
Returns from stack1 instead of stack2 after transfer -> Option D
Quick Check:
Dequeue pops from stack2, not stack1 [OK]
Hint: Dequeue pops from stack2 after transfer [OK]
Common Mistakes:
Returning from wrong stack after transfer
Not transferring elements when stack2 empty
Ignoring empty queue case
5. You want to implement a peek() method for a queue using two stacks that returns the front element without removing it. Which approach is correct?
hard
A. If stack2 is empty, transfer all elements from stack1 to stack2, then return top of stack2
B. Return the bottom element of stack1 directly without transfer
C. Return the top element of stack1 always
D. Transfer elements from stack2 to stack1 and return top of stack1
Solution
Step 1: Understand peek operation in queue
Peek returns the front element without removing it, so it must return the oldest element added.
Step 2: Use stack2 for front element
If stack2 is empty, transfer all elements from stack1 to stack2 to reverse order. Then top of stack2 is front element.
Final Answer:
If stack2 is empty, transfer all elements from stack1 to stack2, then return top of stack2 -> Option A
Quick Check:
Peek uses stack2 top after transfer [OK]
Hint: Peek front from stack2 top after transfer [OK]
Common Mistakes:
Returning bottom of stack1 directly
Returning top of stack1 without transfer
Transferring elements in wrong direction
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.