What is the main difference between a stack and a queue?
A stack follows Last In First Out (LIFO) order, meaning the last item added is the first to be removed. A queue follows First In First Out (FIFO) order, meaning the first item added is the first to be removed.
Click to reveal answer
beginner
When should you use a stack?
Use a stack when you need to reverse things, track recent actions, or manage nested tasks. Examples include undo features, parsing expressions, and backtracking algorithms.
Click to reveal answer
beginner
When is a queue the better choice?
Use a queue when you want to process items in the order they arrive, like in task scheduling, breadth-first search, or handling requests in the order received.
Click to reveal answer
intermediate
How does a stack help in function calls?
A stack keeps track of function calls and returns in the correct order, allowing the program to resume where it left off after a function finishes.
Click to reveal answer
intermediate
Why is a queue useful in breadth-first search (BFS)?
A queue helps BFS explore nodes level by level, ensuring nodes closer to the start are processed before nodes further away.
Click to reveal answer
Which data structure would you use to implement an undo feature?
ALinked List
BStack
CQueue
DHash Table
✗ Incorrect
Undo features require reversing actions, which fits the Last In First Out (LIFO) behavior of a stack.
Which data structure processes elements in the order they arrive?
AStack
BGraph
CTree
DQueue
✗ Incorrect
A queue follows First In First Out (FIFO), so it processes elements in arrival order.
What order does a stack follow?
ARandom
BFIFO
CLIFO
DSorted
✗ Incorrect
Stack follows Last In First Out (LIFO) order.
Which data structure is best for breadth-first search (BFS)?
AQueue
BStack
CHeap
DSet
✗ Incorrect
BFS uses a queue to explore nodes level by level.
If you want to reverse a list of items, which data structure helps most?
AStack
BQueue
CArray
DGraph
✗ Incorrect
Stack reverses order because it removes the last added item first.
Explain when you would choose a stack over a queue and give two real-life examples.
Think about tasks where the last thing done needs to be undone first.
You got /4 concepts.
Describe how a queue works and why it is useful in scheduling tasks.
Imagine people lining up to buy tickets.
You got /5 concepts.
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. Graph
B. Stack
C. Tree
D. Queue
Solution
Step 1: Understand the order requirement
Customers waiting in line are served in the order they arrive, which is First-In-First-Out (FIFO).
Step 2: Match data structure behavior
A queue follows FIFO, so it fits this scenario perfectly.
Final Answer:
Queue -> Option D
Quick Check:
FIFO = Queue [OK]
Hint: Use queue for FIFO order tasks like waiting lines [OK]
Common Mistakes:
Confusing stack (LIFO) with queue (FIFO)
Choosing tree or graph which are not linear
Ignoring order of processing
2. Which of the following is the correct way to push an element '5' onto a stack named myStack in C++ STL?
easy
A. myStack.push(5);
B. myStack.enqueue(5);
C. myStack.insert(5);
D. myStack.add(5);
Solution
Step 1: Recall stack insertion method in C++ STL
The standard method to add an element to a stack is push().
Step 2: Verify the syntax
Using myStack.push(5); correctly adds 5 to the top of the stack.
Final Answer:
myStack.push(5); -> Option A
Quick Check:
Stack insertion = push() [OK]
Hint: Use push() to add elements to a stack in C++ [OK]
Common Mistakes:
Using enqueue which is for queues
Using insert or add which are not stack methods
Confusing stack methods with vector methods
3. What will be the output of the following C++ code snippet?