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 or keep track of recent actions, like undo features, backtracking, or parsing expressions.
Click to reveal answer
beginner
When is a queue the better choice?
Use a queue when you want to process things in the order they arrive, like task scheduling, handling requests, or breadth-first search in graphs.
Click to reveal answer
intermediate
How does a stack help in undo operations?
Each action is pushed onto the stack. When undo is needed, the last action is popped off and reversed, following LIFO order.
Click to reveal answer
intermediate
Why is a queue used in breadth-first search (BFS)?
Because BFS explores nodes level by level, it needs to process nodes in the order they were discovered, which fits the FIFO nature of a queue.
Click to reveal answer
Which data structure removes the most recently added item first?
AQueue
BStack
CLinked List
DTree
✗ Incorrect
Stack uses Last In First Out (LIFO), so it removes the most recently added item first.
Which data structure is best for processing tasks in the order they arrive?
AStack
BHash Table
CGraph
DQueue
✗ Incorrect
Queue uses First In First Out (FIFO), so it processes tasks in the order they arrive.
What is a common use case for a stack?
AUndo functionality
BTask scheduling
CPriority handling
DSearching in a database
✗ Incorrect
Undo functionality relies on reversing the most recent actions, which fits stack's LIFO behavior.
Which data structure would you use for breadth-first search (BFS)?
AQueue
BStack
CSet
DHeap
✗ Incorrect
BFS explores nodes level by level, so it uses a queue to process nodes in the order they are discovered.
If you want to reverse a list of items, which data structure helps most naturally?
AArray
BQueue
CStack
DGraph
✗ Incorrect
Stack reverses order naturally because it removes the last item added first.
Explain the key differences between a stack and a queue and give one example use case for each.
Think about the order items come out and real-life situations where order matters.
You got /3 concepts.
Describe why a queue is used in breadth-first search and why a stack is used in depth-first search.
Consider how nodes are explored in each search method.
You got /3 concepts.
Practice
(1/5)
1. Which data structure should you use when you need to process tasks in the exact order they arrive, like customers waiting in line at a store?
easy
A. Stack
B. Queue
C. Array
D. Tree
Solution
Step 1: Understand the order requirement
Tasks must be processed in the order they arrive, which means first-in, first-out (FIFO).
Step 2: Match the data structure to FIFO
A queue follows FIFO, so it fits the requirement perfectly.
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
Thinking tree fits linear order
2. Which of the following is the correct TypeScript syntax to add an element to the top of a stack implemented as an array named stack?
easy
A. stack.unshift(element);
B. stack.pop(element);
C. stack.shift(element);
D. stack.push(element);
Solution
Step 1: Identify stack operation to add element
Adding to the top of a stack means pushing an element onto it.
Step 2: Match with correct array method
In TypeScript, push() adds an element to the end (top) of the array acting as a stack.
Final Answer:
stack.push(element); -> Option D
Quick Check:
Push adds to stack top [OK]
Hint: Use push() to add on stack top [OK]
Common Mistakes:
Using pop() which removes instead of adds
Using shift() or unshift() which affect queue ends
Confusing push() with unshift()
3. What will be the output of the following TypeScript code?