Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Queue and Stack behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Queue and Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Queue operation?
Consider the following C# code using a Queue. What will be printed to the console?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<int> q = new Queue<int>();
        q.Enqueue(10);
        q.Enqueue(20);
        q.Enqueue(30);
        Console.WriteLine(q.Dequeue());
        Console.WriteLine(q.Peek());
    }
}
A20\n30
B10\n30
C10\n20
D20\n10
Attempts:
2 left
💡 Hint
Remember, Queue follows FIFO: first in, first out.
Predict Output
intermediate
2:00remaining
What is the output of this Stack operation?
Look at this C# code using a Stack. What will be printed?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Stack<string> s = new Stack<string>();
        s.Push("apple");
        s.Push("banana");
        s.Push("cherry");
        Console.WriteLine(s.Pop());
        Console.WriteLine(s.Peek());
    }
}
Acherry\nbanana
Bapple\nbanana
Cbanana\napple
Dcherry\napple
Attempts:
2 left
💡 Hint
Stack follows LIFO: last in, first out.
🔧 Debug
advanced
2:00remaining
Why does this Queue code throw an exception?
This C# code throws an InvalidOperationException at runtime. Why?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<int> q = new Queue<int>();
        q.Enqueue(1);
        q.Dequeue();
        q.Dequeue();
    }
}
ABecause Dequeue is called on an empty Queue after removing the only element.
BBecause Enqueue was not called before Dequeue.
CBecause Queue does not support Dequeue operation.
DBecause the Queue was not initialized properly.
Attempts:
2 left
💡 Hint
Check what happens when you remove more items than exist.
Predict Output
advanced
2:00remaining
What is the final content of the Stack?
After running this C# code, what items remain in the Stack (from top to bottom)?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Stack<int> s = new Stack<int>();
        s.Push(5);
        s.Push(10);
        s.Push(15);
        s.Pop();
        s.Push(20);
        foreach(var item in s) {
            Console.Write(item + " ");
        }
    }
}
A5 10 20
B20 10 5
C10 5 20
D15 20 10 5
Attempts:
2 left
💡 Hint
Remember Pop removes the top item, Push adds on top, and foreach iterates from top to bottom.
🧠 Conceptual
expert
2:00remaining
Which statement correctly describes the difference between Queue and Stack?
Choose the option that best explains the fundamental difference between Queue and Stack data structures.
AQueue and Stack both remove elements randomly without any order.
BStack removes elements in the order they were added; Queue removes elements in reverse order of addition.
CBoth Queue and Stack remove elements in the order they were added, but Stack is faster.
DQueue removes elements in the order they were added; Stack removes elements in reverse order of addition.
Attempts:
2 left
💡 Hint
Think about FIFO vs LIFO.

Practice

(1/5)
1. Which data structure removes elements in the order they were added, like a line at a grocery store?
easy
A. Array
B. Stack
C. Dictionary
D. Queue

Solution

  1. Step 1: Understand FIFO behavior

    A queue removes elements in the order they were added, called First-In-First-Out (FIFO).
  2. Step 2: Match behavior to real-life example

    A line at a grocery store is FIFO, so the queue matches this behavior.
  3. Final Answer:

    Queue -> Option D
  4. Quick Check:

    FIFO = Queue [OK]
Hint: FIFO means first in, first out like a queue line [OK]
Common Mistakes:
  • Confusing stack with queue
  • Thinking stack is FIFO
  • Mixing array behavior with queue
  • Assuming dictionary has order
2. Which of the following is the correct way to add an item to a Stack in C#?
easy
A. stack.Push(item);
B. stack.Enqueue(item);
C. stack.Add(item);
D. stack.Insert(item);

Solution

  1. Step 1: Recall Stack method names

    In C#, Stack uses Push() to add items on top.
  2. Step 2: Identify correct method

    Enqueue is for Queue, Add and Insert are not Stack methods.
  3. Final Answer:

    stack.Push(item); -> Option A
  4. Quick Check:

    Push adds to Stack [OK]
Hint: Use Push() to add to Stack, Enqueue() for Queue [OK]
Common Mistakes:
  • Using Enqueue() on Stack
  • Using Add() or Insert() which don't exist
  • Confusing Stack and Queue methods
  • Syntax errors with method calls
3. What is the output of this C# code?
var stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
Console.WriteLine(stack.Pop());
Console.WriteLine(stack.Peek());
medium
A. 3\n2
B. 1\n2
C. 2\n3
D. 3\n3

Solution

  1. Step 1: Trace Push operations

    Stack after pushes: bottom=1, middle=2, top=3.
  2. Step 2: Execute Pop and Peek

    Pop() removes and returns top (3). Peek() returns new top (2) without removing.
  3. Final Answer:

    3\n2 -> Option A
  4. Quick Check:

    Pop=3, Peek=2 [OK]
Hint: Pop removes top, Peek shows top without removing [OK]
Common Mistakes:
  • Mixing Pop and Peek results
  • Assuming FIFO order
  • Confusing stack order
  • Forgetting Pop removes item
4. Identify the error in this C# code using Queue:
var queue = new Queue<string>();
queue.Push("apple");
queue.Enqueue("banana");
Console.WriteLine(queue.Dequeue());
medium
A. Dequeue() returns last item added
B. Queue does not have Push() method
C. Enqueue() should be Dequeue()
D. Queue cannot store strings

Solution

  1. Step 1: Check Queue methods

    Queue uses Enqueue() to add, not Push().
  2. Step 2: Identify incorrect method usage

    Calling Push() on Queue causes a compile error.
  3. Final Answer:

    Queue does not have Push() method -> Option B
  4. Quick Check:

    Queue uses Enqueue, no Push [OK]
Hint: Queue uses Enqueue(), Stack uses Push() [OK]
Common Mistakes:
  • Using Push() on Queue
  • Confusing Enqueue and Dequeue
  • Thinking Dequeue returns last item
  • Assuming Queue can't hold strings
5. You want to reverse the order of words in a sentence using C#. Which data structure is best and why?
string sentence = "hello world from C#";
hard
A. Queue, because it keeps original order using FIFO
B. Dictionary, because it stores key-value pairs
C. Stack, because it reverses order using LIFO
D. List, because it sorts items automatically

Solution

  1. Step 1: Understand the goal

    Reversing words means last word should come first, so order is reversed.
  2. Step 2: Choose data structure behavior

    Stack uses Last-In-First-Out (LIFO), perfect for reversing order.
  3. Step 3: Eliminate other options

    Queue keeps order (FIFO), Dictionary stores pairs unordered, List does not reverse automatically.
  4. Final Answer:

    Stack, because it reverses order using LIFO -> Option C
  5. Quick Check:

    Reverse order = Stack (LIFO) [OK]
Hint: Use Stack to reverse order with LIFO behavior [OK]
Common Mistakes:
  • Choosing Queue for reversing
  • Thinking List auto-sorts
  • Using Dictionary for order
  • Ignoring LIFO vs FIFO difference