Implement Stack Using Queue in DSA C# - Time & Space Complexity
When we build a stack using a queue, we want to know how fast the main actions like push and pop happen.
We ask: How does the time to do these actions grow as we add more items?
Analyze the time complexity of the push operation in this stack using one queue.
public class StackUsingQueue {
private Queue<int> queue = new Queue<int>();
public void Push(int x) {
queue.Enqueue(x);
int size = queue.Count;
while (size > 1) {
queue.Enqueue(queue.Dequeue());
size--;
}
}
}
This code adds an item to the stack by putting it in the queue and then rotating the queue to keep the newest item at the front.
Look at what repeats when we push:
- Primary operation: Moving items inside the queue by dequeueing and enqueueing.
- How many times: This happens once for each item already in the queue, so it repeats n-1 times if there are n items.
As we add more items, the push operation takes longer because it moves all existing items each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 moves inside the queue |
| 100 | About 99 moves inside the queue |
| 1000 | About 999 moves inside the queue |
Pattern observation: The work grows directly with the number of items, so doubling items doubles the work.
Time Complexity: O(n)
This means pushing an item takes time proportional to how many items are already in the stack.
[X] Wrong: "Push in this stack is always fast because queue operations are fast."
[OK] Correct: Even though queue operations are quick, we do many of them during push, so the total time grows with the number of items.
Understanding how to build one data structure using another and analyzing its speed shows your problem-solving skills and helps you think about trade-offs clearly.
"What if we used two queues instead of one? How would the time complexity of push and pop change?"
Practice
Solution
Step 1: Understand stack and queue behavior
A stack follows Last-In-First-Out (LIFO), while a queue follows First-In-First-Out (FIFO).Step 2: Simulate stack using queue
By reordering elements in a single queue after each push, the newest element is always at the front, simulating stack pop behavior.Final Answer:
Use one queue and reorder elements to simulate stack behavior -> Option BQuick Check:
Stack via queue = reorder elements [OK]
- Confusing stack and queue order
- Using two stacks instead of queues
- Ignoring element reordering after push
Solution
Step 1: Review C# generic queue declaration
In C#, a generic queue is declared as Queue<T> with new keyword and parentheses.Step 2: Check each option
Queue<int> queue = new Queue<int>(); correctly uses Queue<int> queue = new Queue<int>(); which is valid syntax.Final Answer:
Queue<int> queue = new Queue<int>(); -> Option AQuick Check:
Generic queue syntax = Queue<int> queue = new Queue<int>(); [OK]
- Omitting new keyword
- Missing generic type in constructor
- Using non-generic Queue without type
var stack = new StackUsingQueue(); stack.Push(10); stack.Push(20); stack.Push(30); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Pop());
Solution
Step 1: Understand push operation in stack using queue
Each push adds element and reorders queue so last pushed is at front.Step 2: Trace pop operations
First pop returns 30 (last pushed), second pop returns 20 (second last pushed).Final Answer:
30\n20 -> Option DQuick Check:
Stack pop order = last in first out = 30, then 20 [OK]
- Assuming queue FIFO order for pop
- Mixing push and pop order
- Ignoring reorder after push
public void Push(int x) {
queue.Enqueue(x);
for (int i = 0; i < queue.Count - 1; i++) {
queue.Enqueue(queue.Dequeue());
}
}Solution
Step 1: Analyze loop condition with queue.Count
queue.Count changes momentarily during iteration (decreases after Dequeue, restores after Enqueue).Step 2: Explain why this is a concern
Although the size restores before the next condition check and works correctly, using a changing value in the loop condition can be confusing and is bad practice. Store count beforehand: int n = queue.Count; for(int i=0; i < n-1; i++)...Final Answer:
Using queue.Count in loop condition changes during iteration -> Option CQuick Check:
Loop count must be stored before loop [OK]
- Using dynamic queue.Count in loop condition
- Not rotating queue elements correctly
- Confusing enqueue and dequeue operations
Solution
Step 1: Understand two-queue stack implementation
One queue holds elements; pop requires removing last pushed element, which is at the end.Step 2: Describe correct pop operation
Move all but last element from queue1 to queue2, dequeue last from queue1 (stack pop), then swap queues for next operations.Final Answer:
Push elements into queue1; for pop, move all but last from queue1 to queue2, dequeue last from queue1, then swap queues -> Option AQuick Check:
Pop = move all but last, dequeue last, swap queues [OK]
- Dequeuing directly without moving elements
- Not swapping queues after pop
- Confusing which queue holds elements
Ready to prove it?
Pass the test and this pattern moves unknown
