📖 Scenario: Imagine a line at a coffee shop where customers wait their turn to order. The first customer to arrive is the first to be served. This is called the FIFO (First In, First Out) principle, which queues follow.
🎯 Goal: You will create a queue of customers waiting to order coffee. Then, you will add customers to the queue, remove the first customer served, and finally display the order of customers still waiting.
📋 What You'll Learn
Create a queue called coffeeQueue with the customers: "Alice", "Bob", "Charlie"
Create a variable called newCustomer and set it to "Diana"
Add newCustomer to coffeeQueue
Remove the first customer from coffeeQueue
Print the remaining customers in coffeeQueue in order, separated by " -> "
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage waiting lines, like customers at a store or tasks waiting to be processed by a computer.
💼 Career
Understanding queues and FIFO is important for software developers working on scheduling, task management, and real-time systems.
Progress0 / 4 steps
1
Create the initial queue
Create a queue called coffeeQueue and add these customers in order: "Alice", "Bob", "Charlie"
DSA C#
Hint
Use Queue<string> and Enqueue method to add customers.
2
Add a new customer
Create a variable called newCustomer and set it to "Diana"
DSA C#
Hint
Declare a string variable newCustomer and assign it the value "Diana".
3
Add the new customer to the queue and remove the first served
Add newCustomer to coffeeQueue using Enqueue, then remove the first customer from coffeeQueue using Dequeue
DSA C#
Hint
Use Enqueue to add and Dequeue to remove the first customer.
4
Print the remaining customers in the queue
Print the customers in coffeeQueue in order separated by " -> " using string.Join
DSA C#
Hint
Use Console.WriteLine(string.Join(" -> ", coffeeQueue)) to print the queue.
Practice
(1/5)
1. What does the FIFO principle mean in the context of a Queue data structure?
easy
A. Last In, Last Out - the last element added is the last removed
B. First In, First Out - the first element added is the first removed
C. Last In, First Out - the last element added is the first removed
D. First In, Last Out - the first element added is the last removed
Solution
Step 1: Understand FIFO meaning
FIFO stands for First In, First Out, meaning the first element added to the queue is the first one to be removed.
Step 2: Apply FIFO to Queue
In a queue, elements are processed in the order they arrive, so the earliest added element leaves first.
Final Answer:
First In, First Out - the first element added is the first removed -> Option B
Quick Check:
FIFO = First In, First Out [OK]
Hint: Remember queue = line; first come, first served [OK]
Common Mistakes:
Confusing FIFO with stack behavior (LIFO)
Thinking last added element is removed first
Mixing up terms In and Out order
2. Which of the following is the correct way to add an element to a Queue<int> named q in C#?
easy
A. q.Insert(5);
B. q.Push(5);
C. q.Add(5);
D. q.Enqueue(5);
Solution
Step 1: Recall Queue method to add elements
The method to add elements to a Queue in C# is Enqueue.
Step 2: Check each option
Only q.Enqueue(5); is valid syntax to add 5 to the queue.
Final Answer:
q.Enqueue(5); -> Option D
Quick Check:
Use Enqueue to add in Queue [OK]
Hint: Use Enqueue to add, Dequeue to remove [OK]
Common Mistakes:
Using Push which is for stacks
Using Add or Insert which are not Queue methods
Confusing method names with List
3. What will be the output of the following C# code?
Queue<string> q = new Queue<string>();
q.Enqueue("apple");
q.Enqueue("banana");
q.Enqueue("cherry");
string first = q.Dequeue();
q.Enqueue("date");
Console.WriteLine(string.Join(", ", q));
medium
A. "banana, cherry, date"
B. "apple, banana, cherry, date"
C. "date, apple, banana, cherry"
D. "cherry, date, banana"
Solution
Step 1: Trace Enqueue and Dequeue operations
Initially queue: [apple, banana, cherry]. Dequeue removes "apple" (first in). Queue now: [banana, cherry]. Then Enqueue "date" adds it to the end: [banana, cherry, date].
Step 2: Output the queue contents
Printing queue elements joined by comma results in "banana, cherry, date".
Final Answer:
"banana, cherry, date" -> Option A
Quick Check:
Dequeue removes first, Enqueue adds last [OK]
Hint: Dequeue removes front, Enqueue adds back [OK]
Common Mistakes:
Forgetting Dequeue removes first element
Assuming Enqueue adds to front
Printing queue before Dequeue
4. Identify the error in the following C# code that uses a Queue<int>:
Queue<int> q = new Queue<int>();
q.Enqueue(10);
q.Enqueue(20);
int x = q.Pop();
Console.WriteLine(x);
medium
A. Pop() method does not exist for Queue
B. Enqueue should be Add
C. Queue cannot store integers
D. Missing using System.Collections.Generic;
Solution
Step 1: Check Queue methods
Queue in C# does not have a Pop() method; it uses Dequeue() to remove elements.
Step 2: Validate other parts
Enqueue is correct to add elements; Queue can store integers; using System.Collections.Generic; is needed but not an error here.
Final Answer:
Pop() method does not exist for Queue -> Option A
Quick Check:
Use Dequeue, not Pop, for Queue removal [OK]
Hint: Queue uses Dequeue, not Pop [OK]
Common Mistakes:
Using Pop() from stack on queue
Confusing Enqueue with Add
Ignoring missing namespace errors
5. You have a queue of customer names: ["Anna", "Bob", "Cara"]. You want to process the first two customers and then add "Dave" and "Eva" to the queue. What will be the final queue order after these operations?
hard
A. ["Anna", "Bob", "Dave", "Eva"]
B. ["Dave", "Eva", "Cara"]
C. ["Cara", "Dave", "Eva"]
D. ["Bob", "Cara", "Dave", "Eva"]
Solution
Step 1: Remove first two customers using Dequeue
Removing "Anna" and "Bob" leaves the queue with ["Cara"].
Step 2: Add "Dave" and "Eva" using Enqueue
Adding "Dave" and "Eva" to the end results in ["Cara", "Dave", "Eva"].
Final Answer:
["Cara", "Dave", "Eva"] -> Option C
Quick Check:
Dequeue twice, then Enqueue twice [OK]
Hint: Dequeue removes front, Enqueue adds back [OK]
Common Mistakes:
Not removing both customers before adding
Adding new customers to front instead of back
Confusing order of elements after operations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.