A stack is like a pile of plates where you can only add or remove the top plate. It follows Last In, First Out (LIFO) order.
Click to reveal answer
beginner
Why do we need a stack data structure?
Stacks help manage tasks that need to be done in reverse order, like undo actions or keeping track of function calls.
Click to reveal answer
intermediate
How does a stack solve the problem of function calls in programming?
It keeps track of which function called another, so when a function finishes, the program knows where to return next.
Click to reveal answer
intermediate
What problem does a stack solve in expression evaluation?
Stacks help evaluate math expressions by storing operators and operands in the right order to calculate results correctly.
Click to reveal answer
beginner
What real-life example can help understand a stack?
Think of a stack of books: you can only take the top book off or put a new book on top, not from the middle or bottom.
Click to reveal answer
What order does a stack follow?
ALast In, First Out (LIFO)
BFirst In, First Out (FIFO)
CRandom Order
DSorted Order
✗ Incorrect
A stack always removes the most recently added item first, which is Last In, First Out.
Which problem is best solved by using a stack?
ASorting numbers
BFinding the shortest path
CTracking function calls
DSearching in a list
✗ Incorrect
Stacks keep track of function calls so the program knows where to return after each call.
What happens when you try to remove an item from an empty stack?
AStack resets
BUnderflow error
CNothing happens
DOverflow error
✗ Incorrect
Removing from an empty stack causes an underflow error because there is nothing to remove.
Which real-life example best describes a stack?
AA queue at a ticket counter
BA list of names sorted alphabetically
CA bookshelf with random access
DA pile of plates
✗ Incorrect
A pile of plates is a perfect example of a stack where you add or remove only the top plate.
How does a stack help in expression evaluation?
ABy storing operators and operands in order
BBy sorting numbers
CBy searching for values
DBy reversing the expression
✗ Incorrect
Stacks store operators and operands to evaluate expressions correctly following the right order.
Explain why a stack is useful for managing function calls in programming.
Think about how the program remembers where to go back after a function finishes.
You got /4 concepts.
Describe a real-life situation where a stack-like behavior is used and why it fits the stack concept.
Imagine a pile of objects where you can only take or add the top one.
You got /3 concepts.
Practice
(1/5)
1. Why do we use a stack data structure in programming?
easy
A. To keep track of function calls and return addresses
B. To store data in random order
C. To sort elements automatically
D. To store large amounts of data permanently
Solution
Step 1: Understand the role of stack in function calls
Stacks help keep track of where each function should return after it finishes executing, by storing return addresses.
Step 2: Compare with other options
Stacks do not sort data automatically, nor store data randomly or permanently; their main use is managing function calls.
Final Answer:
To keep track of function calls and return addresses -> Option A
Quick Check:
Stack = function call tracking [OK]
Hint: Stack remembers last function called first [OK]
Common Mistakes:
Thinking stack sorts data automatically
Confusing stack with permanent storage
Assuming stack stores data randomly
2. Which of the following is the correct way to push an element onto a stack in Rust using a vector?
easy
A. stack.insert(element);
B. stack.add(element);
C. stack.append(element);
D. stack.push(element);
Solution
Step 1: Recall Rust vector methods for stack operations
In Rust, a vector's push() method adds an element to the end, simulating stack push.
Step 2: Check other options for correctness
Methods add(), insert(), and append() do not exist or do not behave like push() for stack usage.
Final Answer:
stack.push(element); -> Option D
Quick Check:
Use push() to add element to stack [OK]
Hint: Use push() to add on top of stack [OK]
Common Mistakes:
Using add() which is not a Rust Vec method
Confusing insert() which needs index
Using append() which merges vectors
3. What will be the output of the following Rust code snippet?
let mut stack = Vec::new();
stack.push(10);
stack.push(20);
let top = stack.pop();
println!("{:?}", top);
medium
A. Some(20)
B. Some(10)
C. 20
D. None
Solution
Step 1: Trace stack push operations
First 10 is pushed, then 20 is pushed. Stack now has [10, 20].
Step 2: Understand pop() behavior
pop() removes and returns the last element, which is 20, wrapped in Some(20) because pop returns Option.
Final Answer:
Some(20) -> Option A
Quick Check:
pop() returns last element wrapped in Some [OK]
Hint: pop() returns Option with last pushed element [OK]
Common Mistakes:
Expecting pop() to return raw value, not Option
Confusing order of elements pushed
Thinking pop() returns None when stack is not empty
4. Identify the error in this Rust code that uses a stack:
let mut stack = Vec::new();
let top = stack.pop().unwrap();
println!("{}", top);
medium
A. println! macro syntax is incorrect
B. Calling unwrap() on empty stack causes panic
C. stack should be immutable
D. pop() method does not exist on Vec
Solution
Step 1: Analyze pop() on empty vector
pop() returns None if vector is empty. Calling unwrap() on None causes panic (runtime error).
Step 2: Check other options for validity
pop() exists on Vec, stack is mutable, and println! syntax is correct.
Final Answer:
Calling unwrap() on empty stack causes panic -> Option B
Quick Check:
unwrap() on None panics [OK]
Hint: Never unwrap pop() without checking empty [OK]
Common Mistakes:
Ignoring possibility of empty stack
Thinking pop() is undefined on Vec
Assuming println! syntax is wrong
5. You want to reverse a list of integers using a stack in Rust. Which approach correctly uses a stack to reverse the list?
let list = vec![1, 2, 3, 4];
let mut stack = Vec::new();
// Fill stack
for &item in &list {
stack.push(item);
}
// Extract reversed list
let mut reversed = Vec::new();
while let Some(val) = stack.pop() {
reversed.push(val);
}
println!("{:?}", reversed);
hard
A. Stack is not needed; list.reverse() is better
B. pop() should be replaced with peek() to reverse
C. This code correctly reverses the list using a stack
D. The code will panic because stack is empty
Solution
Step 1: Understand pushing all elements onto stack
Each element from list is pushed onto stack in order: 1,2,3,4.
Step 2: Popping elements reverses order
pop() removes last pushed element first, so reversed vector gets 4,3,2,1.
Step 3: Confirm no panic occurs
while let Some(val) safely handles empty stack, no panic occurs.
Final Answer:
This code correctly reverses the list using a stack -> Option C
Quick Check:
Stack pop reverses order [OK]
Hint: Push all, then pop all to reverse [OK]
Common Mistakes:
Replacing pop() with peek() which doesn't remove
Assuming code panics on empty stack
Ignoring that stack reverses order naturally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.