📖 Scenario: Imagine you are managing a line at a coffee shop and also handling a stack of plates in the kitchen. You want to understand how to organize these tasks using the right data structures.
🎯 Goal: You will create a simple Java program to simulate adding and removing customers in a queue (line) and adding and removing plates in a stack. This will help you see when to use a queue and when to use a stack.
📋 What You'll Learn
Create a queue to hold customer names in the order they arrive
Create a stack to hold plate numbers in the order they are stacked
Add customers to the queue and plates to the stack
Remove a customer from the queue (first come, first served)
Remove a plate from the stack (last placed, first removed)
Print the state of the queue and stack after operations
💡 Why This Matters
🌍 Real World
Queues are used in places like lines at stores or print jobs waiting to print. Stacks are used in undo features or managing plates in a kitchen.
💼 Career
Understanding when to use queues or stacks helps in software development tasks like task scheduling, parsing, and managing resources efficiently.
Progress0 / 4 steps
1
Create the initial data structures
Create a Queue<String> called customerQueue using LinkedList<>() and a Stack<Integer> called plateStack.
DSA Java
Hint
Use Queue<String> customerQueue = new LinkedList<>(); and Stack<Integer> plateStack = new Stack<>();
2
Add customers and plates
Add these customers to customerQueue in this order: "Alice", "Bob", "Charlie". Add these plates to plateStack in this order: 101, 102, 103.
DSA Java
Hint
Use add() to add customers to the queue and push() to add plates to the stack.
3
Remove one customer and one plate
Remove one customer from customerQueue using poll() and remove one plate from plateStack using pop().
DSA Java
Hint
Use poll() to remove the first customer and pop() to remove the last plate.
4
Print the current state of queue and stack
Print the current contents of customerQueue and plateStack using System.out.println().
DSA Java
Hint
Use System.out.println("Customers in queue: " + customerQueue); and System.out.println("Plates in stack: " + plateStack);
Practice
(1/5)
1. Which data structure should you use when you need to process elements in the exact order they arrive, like customers waiting in line?
easy
A. Stack
B. Queue
C. Array
D. HashMap
Solution
Step 1: Understand the order requirement
Processing elements in the exact order they arrive means first-in, first-out (FIFO).
Step 2: Match data structure behavior
Queue follows FIFO, so it processes elements in the order they come.
Final Answer:
Queue -> Option B
Quick Check:
FIFO = Queue [OK]
Hint: Use Queue for first-in, first-out order [OK]
Common Mistakes:
Confusing Stack (LIFO) with Queue (FIFO)
Choosing Array without order guarantees
Using HashMap which has no order
2. Which of the following is the correct way to add an element to a Stack in Java?
easy
A. stack.push(element);
B. stack.enqueue(element);
C. stack.addLast(element);
D. stack.insert(element);
Solution
Step 1: Recall Stack method to add elements
In Java, Stack uses push() to add elements on top.
Step 2: Check other options
enqueue() is for Queue, addLast() is for Deque, insert() is not a Stack method.
Final Answer:
stack.push(element); -> Option A
Quick Check:
Stack add = push() [OK]
Hint: Use push() to add elements to Stack [OK]
Common Mistakes:
Using enqueue() which is for Queue
Using addLast() which is for Deque
Using insert() which does not exist in Stack
3. What will be the output of the following Java code?
import java.util.Stack;
public class Test {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack.pop());
System.out.println(stack.peek());
}
}
medium
A. 30
10
B. 10
20
C. 30
20
D. 20
30
Solution
Step 1: Trace push operations
Elements pushed in order: 10 (bottom), 20, 30 (top).
Step 2: Trace pop and peek
pop() removes and returns top: 30. peek() returns new top without removing: 20.
Final Answer:
30\n20 -> Option C
Quick Check:
pop = 30, peek = 20 [OK]
Hint: pop() removes top, peek() shows top without removing [OK]
Common Mistakes:
Confusing pop() and peek() results
Assuming FIFO order instead of LIFO
Mixing push order with pop output
4. Identify the error in this Java code snippet using a Queue:
import java.util.Queue;
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.push(5);
queue.add(10);
System.out.println(queue.poll());
}
}
medium
A. Queue interface has no push() method
B. LinkedList cannot be used as Queue
C. poll() does not remove elements
D. add() method is invalid for Queue
Solution
Step 1: Check Queue methods
Queue interface does not have push() method; push() is for Stack.
Step 2: Validate other methods
LinkedList can be used as Queue, poll() removes and returns head, add() is valid for Queue.
Final Answer:
Queue interface has no push() method -> Option A
Quick Check:
Queue push() invalid [OK]
Hint: Queue uses add(), not push() [OK]
Common Mistakes:
Using push() on Queue instead of add()
Thinking poll() does not remove element
Believing LinkedList can't implement Queue
5. You need to design a system that tracks the most recent 5 user actions to allow undo operations. Which data structure is best and why?
hard
A. Queue, because it processes actions in order
B. HashMap, because it stores actions with keys
C. ArrayList, because it allows random access
D. Stack, because it reverses the order for undo
Solution
Step 1: Understand undo operation behavior
Undo requires reversing the order of actions: last action undone first.
Step 2: Match data structure to behavior
Stack follows last-in, first-out (LIFO), perfect for undo functionality.
Final Answer:
Stack, because it reverses the order for undo -> Option D
Quick Check:
Undo = LIFO = Stack [OK]
Hint: Undo needs LIFO order, so use Stack [OK]
Common Mistakes:
Choosing Queue which is FIFO, not suitable for undo
Using HashMap which has no order
Thinking ArrayList automatically reverses order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.