Bird
Raised Fist0
DSA C#programming~20 mins

Implement Stack Using Queue in DSA C# - 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 pushing and popping elements?

Consider a stack implemented using two queues. After the following operations, what is the printed stack content?

Push(1)
Push(2)
Pop()
Push(3)

Print the stack from top to bottom.

DSA C#
class StackUsingQueues {
    private Queue<int> q1 = new Queue<int>();
    private Queue<int> q2 = new Queue<int>();

    public void Push(int x) {
        q2.Enqueue(x);
        while (q1.Count > 0) {
            q2.Enqueue(q1.Dequeue());
        }
        var temp = q1;
        q1 = q2;
        q2 = temp;
    }

    public int Pop() {
        if (q1.Count == 0) throw new InvalidOperationException("Stack is empty");
        return q1.Dequeue();
    }

    public string PrintStack() {
        return string.Join(" -> ", q1.Select(i => i.ToString())) + " -> null";
    }
}

var stack = new StackUsingQueues();
stack.Push(1);
stack.Push(2);
stack.Pop();
stack.Push(3);
Console.WriteLine(stack.PrintStack());
A3 -> 1 -> null
B1 -> 3 -> null
C2 -> 3 -> null
D3 -> 2 -> null
Attempts:
2 left
💡 Hint

Remember that the newest pushed element is always at the front of the queue after push.

🧠 Conceptual
intermediate
1:00remaining
Which queue operation is used to simulate stack's pop?

In a stack implemented using two queues, which queue operation corresponds to the stack's pop operation?

AEnqueue on the first queue
BPeek the last element in the second queue
CDequeue from the first queue
DClear the second queue
Attempts:
2 left
💡 Hint

Pop removes the top element from the stack.

🔧 Debug
advanced
2:00remaining
Identify the error in this stack push method using one queue

What error will occur when running this push method for stack using one queue?

public void Push(int x) {
    q.Enqueue(x);
    for (int i = 0; i < q.Count - 1; i++) {
        q.Enqueue(q.Dequeue());
    }
}
DSA C#
Queue<int> q = new Queue<int>();

public void Push(int x) {
    q.Enqueue(x);
    for (int i = 0; i < q.Count - 1; i++) {
        q.Enqueue(q.Dequeue());
    }
}
ASyntax error due to missing semicolon
BRuntime error due to modifying queue during iteration
CInfinite loop because q.Count changes inside the loop
DNo error, works correctly
Attempts:
2 left
💡 Hint

Observe that each iteration dequeues and enqueues one element, keeping q.Count constant.

📝 Syntax
advanced
1:30remaining
What is the output of this stack pop method using two queues?

Given this pop method, what is the output when the stack contains [3, 2, 1] (top to bottom) and pop is called once?

public int Pop() {
    while (q1.Count > 1) {
        q2.Enqueue(q1.Dequeue());
    }
    int popped = q1.Dequeue();
    var temp = q1;
    q1 = q2;
    q2 = temp;
    return popped;
}
DSA C#
Queue<int> q1 = new Queue<int>(new[] {3, 2, 1});
Queue<int> q2 = new Queue<int>();

public int Pop() {
    while (q1.Count > 1) {
        q2.Enqueue(q1.Dequeue());
    }
    int popped = q1.Dequeue();
    var temp = q1;
    q1 = q2;
    q2 = temp;
    return popped;
}

Console.WriteLine(Pop());
A1
B3
C2
DRuntime error
Attempts:
2 left
💡 Hint

The last element enqueued is at the end of q1, so pop removes it.

🚀 Application
expert
2:30remaining
How many elements remain after these operations on stack using one queue?

Starting with an empty stack implemented using one queue, perform these operations:

Push(10)
Push(20)
Pop()
Push(30)
Pop()

How many elements remain in the stack?

DSA C#
Queue<int> q = new Queue<int>();

void Push(int x) {
    q.Enqueue(x);
    int size = q.Count;
    for (int i = 0; i < size - 1; i++) {
        q.Enqueue(q.Dequeue());
    }
}

int Pop() {
    if (q.Count == 0) throw new InvalidOperationException("Stack empty");
    return q.Dequeue();
}

Push(10);
Push(20);
Pop();
Push(30);
Pop();

int remaining = q.Count;
Console.WriteLine(remaining);
A3
B1
C2
D0
Attempts:
2 left
💡 Hint

Trace each push and pop carefully, noting how the queue changes.

Practice

(1/5)
1. What is the main idea behind implementing a stack using a queue?
easy
A. Use a linked list directly to implement stack
B. Use one queue and reorder elements to simulate stack behavior
C. Use two stacks to simulate queue behavior
D. Use an array to simulate queue behavior

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

    By reordering elements in a single queue after each push, the newest element is always at the front, simulating stack pop behavior.
  3. Final Answer:

    Use one queue and reorder elements to simulate stack behavior -> Option B
  4. Quick Check:

    Stack via queue = reorder elements [OK]
Hint: Remember stack is LIFO, queue is FIFO; reorder queue to mimic stack [OK]
Common Mistakes:
  • Confusing stack and queue order
  • Using two stacks instead of queues
  • Ignoring element reordering after push
2. Which of the following is the correct C# syntax to declare a queue of integers?
easy
A. Queue<int> queue = new Queue<int>();
B. Queue queue = new Queue<int>();
C. Queue<int> queue = Queue<int>();
D. Queue<int> queue = new Queue();

Solution

  1. Step 1: Review C# generic queue declaration

    In C#, a generic queue is declared as Queue<T> with new keyword and parentheses.
  2. Step 2: Check each option

    Queue<int> queue = new Queue<int>(); correctly uses Queue<int> queue = new Queue<int>(); which is valid syntax.
  3. Final Answer:

    Queue<int> queue = new Queue<int>(); -> Option A
  4. Quick Check:

    Generic queue syntax = Queue<int> queue = new Queue<int>(); [OK]
Hint: Use new Queue<int>() to declare generic queue in C# [OK]
Common Mistakes:
  • Omitting new keyword
  • Missing generic type in constructor
  • Using non-generic Queue without type
3. What will be the output of the following C# code implementing stack using one queue?
var stack = new StackUsingQueue();
stack.Push(10);
stack.Push(20);
stack.Push(30);
Console.WriteLine(stack.Pop());
Console.WriteLine(stack.Pop());
medium
A. 10\n30
B. 10\n20
C. 20\n30
D. 30\n20

Solution

  1. Step 1: Understand push operation in stack using queue

    Each push adds element and reorders queue so last pushed is at front.
  2. Step 2: Trace pop operations

    First pop returns 30 (last pushed), second pop returns 20 (second last pushed).
  3. Final Answer:

    30\n20 -> Option D
  4. Quick Check:

    Stack pop order = last in first out = 30, then 20 [OK]
Hint: Pop returns last pushed element due to queue reorder [OK]
Common Mistakes:
  • Assuming queue FIFO order for pop
  • Mixing push and pop order
  • Ignoring reorder after push
4. Identify the potential issue in this C# method for stack push using one queue:
public void Push(int x) {
    queue.Enqueue(x);
    for (int i = 0; i < queue.Count - 1; i++) {
        queue.Enqueue(queue.Dequeue());
    }
}
medium
A. Enqueue should be Dequeue
B. Missing return statement
C. Using queue.Count in loop condition changes during iteration
D. Loop should run queue.Count times

Solution

  1. Step 1: Analyze loop condition with queue.Count

    queue.Count changes momentarily during iteration (decreases after Dequeue, restores after Enqueue).
  2. 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++)...
  3. Final Answer:

    Using queue.Count in loop condition changes during iteration -> Option C
  4. Quick Check:

    Loop count must be stored before loop [OK]
Hint: Store queue.Count before loop to avoid changing condition [OK]
Common Mistakes:
  • Using dynamic queue.Count in loop condition
  • Not rotating queue elements correctly
  • Confusing enqueue and dequeue operations
5. You want to implement a stack using two queues. Which approach correctly simulates the stack's pop operation?
hard
A. Push elements into queue1; for pop, move all but last from queue1 to queue2, dequeue last from queue1, then swap queues
B. Push elements into queue2; for pop, dequeue directly from queue2
C. Push elements into queue1; pop by dequeuing from queue1 without moving elements
D. Push elements into queue2; for pop, move all elements to queue1 and dequeue first

Solution

  1. Step 1: Understand two-queue stack implementation

    One queue holds elements; pop requires removing last pushed element, which is at the end.
  2. 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.
  3. Final Answer:

    Push elements into queue1; for pop, move all but last from queue1 to queue2, dequeue last from queue1, then swap queues -> Option A
  4. Quick Check:

    Pop = move all but last, dequeue last, swap queues [OK]
Hint: Pop by moving all but last element, then dequeue last [OK]
Common Mistakes:
  • Dequeuing directly without moving elements
  • Not swapping queues after pop
  • Confusing which queue holds elements
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