A Stack is a collection where elements are added and removed from the top only. It follows Last In, First Out (LIFO) order, like a stack of plates where you take the top plate first.
Click to reveal answer
beginner
What is a Queue in data structures?
A Queue is a collection where elements are added at the back and removed from the front. It follows First In, First Out (FIFO) order, like a line at a ticket counter where the first person in line is served first.
Click to reveal answer
intermediate
When should you use a Stack?
Use a Stack when you need to reverse things, track recent actions, or manage nested tasks. Examples: undo buttons, backtracking in games, or parsing expressions.
Click to reveal answer
intermediate
When should you use a Queue?
Use a Queue when you want to process things in the order they arrive. Examples: printing tasks, customer service lines, or breadth-first search in graphs.
Click to reveal answer
beginner
What is the main difference between Stack and Queue?
Stack removes the most recent item added (LIFO), while Queue removes the oldest item added (FIFO). This difference decides which one to use based on the problem's order needs.
Click to reveal answer
Which data structure follows Last In, First Out (LIFO)?
AArray
BQueue
CStack
DLinked List
✗ Incorrect
Stack follows LIFO, meaning the last element added is the first to be removed.
Which data structure is best for managing tasks in the order they arrive?
AQueue
BGraph
CTree
DStack
✗ Incorrect
Queue processes elements in First In, First Out (FIFO) order, perfect for tasks arriving in sequence.
What real-life example best represents a Queue?
AA stack of books
BA line at a grocery store
CA pile of plates
DA nested folder structure
✗ Incorrect
A line at a grocery store is a queue where the first person to arrive is served first.
Which data structure would you use for an undo feature in a text editor?
AQueue
BHash Table
CGraph
DStack
✗ Incorrect
Undo features use Stack to reverse recent actions in LIFO order.
If you want to process print jobs in the order they were sent, which data structure fits best?
AQueue
BStack
CSet
DTree
✗ Incorrect
Queue processes print jobs in the order they arrive (FIFO).
Explain the main difference between a Stack and a Queue and give one example use case for each.
Think about how items are added and removed in each.
You got /3 concepts.
Describe a real-life situation where using a Queue is better than a Stack and why.
Consider situations where fairness and order are important.
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. Graph
B. Stack
C. Tree
D. Queue
Solution
Step 1: Understand the order of processing
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 D
Quick Check:
FIFO = Queue [OK]
Hint: Use queue for first-in, first-out order [OK]
Common Mistakes:
Choosing stack which is last-in, first-out
Confusing queue with tree or graph
Thinking stack can process in arrival order
2. Which of the following is the correct way to add an element to a stack in JavaScript using an array named stack?
easy
A. stack.unshift(element);
B. stack.shift(element);
C. stack.push(element);
D. stack.pop(element);
Solution
Step 1: Identify stack insertion method
Stacks add elements on top, so we use push() to add at the end of the array.
Step 2: Check JavaScript array methods
push() adds element at the end, shift() removes from front, unshift() adds at front, pop() removes from end.
Final Answer:
stack.push(element); -> Option C
Quick Check:
Stack add = push() [OK]
Hint: Use push() to add on top of stack [OK]
Common Mistakes:
Using shift() which removes from front
Using unshift() which adds at front (queue behavior)
Using pop() which removes element
3. What will be the output of the following JavaScript code?