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
Queue and Stack behavior
📖 Scenario: Imagine you are managing a line of customers waiting for service and a pile of books to be processed. You will use a queue to represent the line (first come, first served) and a stack to represent the pile of books (last in, first out).
🎯 Goal: You will create a queue and a stack, add some items to each, then remove items to see how the order changes based on queue and stack behavior.
📋 What You'll Learn
Create a queue of strings called customerQueue with these customers in order: "Alice", "Bob", "Charlie"
Create a stack of strings called bookStack with these books in order: "Book1", "Book2", "Book3"
Dequeue one customer from customerQueue and pop one book from bookStack
Print the dequeued customer and popped book
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting at a store. Stacks are used when you need to reverse order, like undo actions or processing recent items first.
💼 Career
Understanding queues and stacks is important for programming jobs because these data structures help solve many problems involving order and processing sequences.
Progress0 / 4 steps
1
Create the queue and stack with initial items
Create a Queue<string> called customerQueue and add "Alice", "Bob", and "Charlie" in that order. Also create a Stack<string> called bookStack and add "Book1", "Book2", and "Book3" in that order.
C Sharp (C#)
Hint
Use Enqueue to add items to the queue and Push to add items to the stack.
2
Create variables to hold removed items
Create two string variables: servedCustomer and processedBook to hold the items you will remove from the queue and stack.
C Sharp (C#)
Hint
Just declare two string variables to hold the removed items.
3
Remove one item from the queue and stack
Remove one customer from customerQueue using Dequeue() and assign it to servedCustomer. Remove one book from bookStack using Pop() and assign it to processedBook.
C Sharp (C#)
Hint
Use Dequeue() to remove from the queue and Pop() to remove from the stack.
4
Print the removed items
Print the values of servedCustomer and processedBook using two separate Console.WriteLine statements.
C Sharp (C#)
Hint
Use Console.WriteLine(servedCustomer) and Console.WriteLine(processedBook) to show the removed items.
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
Step 1: Understand FIFO behavior
A queue removes elements in the order they were added, called First-In-First-Out (FIFO).
Step 2: Match behavior to real-life example
A line at a grocery store is FIFO, so the queue matches this behavior.
Final Answer:
Queue -> Option D
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
Step 1: Recall Stack method names
In C#, Stack uses Push() to add items on top.
Step 2: Identify correct method
Enqueue is for Queue, Add and Insert are not Stack methods.
Final Answer:
stack.Push(item); -> Option A
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
Step 1: Trace Push operations
Stack after pushes: bottom=1, middle=2, top=3.
Step 2: Execute Pop and Peek
Pop() removes and returns top (3). Peek() returns new top (2) without removing.
Final Answer:
3\n2 -> Option A
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());