A stack is like a pile of plates where you can only add or remove the top plate. It follows the Last In, First Out (LIFO) rule.
Click to reveal answer
beginner
Why do we need a stack in programming?
Stacks help manage tasks that need to be done in reverse order, like undo actions, function calls, or checking matching brackets.
Click to reveal answer
intermediate
How does a stack solve the problem of function calls in programming?
When functions call other functions, the stack keeps track of where to return after each call, ensuring the last called function finishes first.
Click to reveal answer
intermediate
What problem does a stack solve in expression evaluation?
Stacks help evaluate expressions by storing operators and operands, especially when dealing with parentheses and operator precedence.
Click to reveal answer
beginner
Explain how a stack helps in undo functionality.
Each action is pushed onto the stack. When undo is pressed, the last action is popped and reversed, allowing step-by-step undoing.
Click to reveal answer
What order does a stack follow?
ASorted Order
BLast In, First Out (LIFO)
CRandom Order
DFirst In, First Out (FIFO)
✗ 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?
ATracking function calls
BSorting numbers
CSearching in a list
DStoring unique items
✗ Incorrect
Stacks keep track of function calls and return points, making them ideal for this problem.
What happens when you pop from an empty stack?
AYou get an error or no value
BYou get the first item added
CYou get the last item added
DThe stack resets
✗ Incorrect
Popping from an empty stack usually causes an error or returns no value because there is nothing to remove.
How does a stack help with undo operations?
ABy sorting actions alphabetically
BBy storing only the first action
CBy deleting all actions at once
DBy storing actions in order and reversing the last one
✗ Incorrect
Undo works by reversing the last action stored on the stack, following LIFO.
Which of these is NOT a typical use of a stack?
AEvaluating expressions
BFunction call management
CBreadth-first search traversal
DUndo functionality
✗ Incorrect
Breadth-first search uses a queue, not a stack.
Describe in your own words why a stack is useful in programming and give two examples of problems it solves.
Think about tasks that need to be reversed or tracked in order.
You got /4 concepts.
Explain how a stack manages function calls and why this is important for a program to run correctly.
Imagine stacking books and removing the top one first.
You got /3 concepts.
Practice
(1/5)
1. Why do we use a stack data structure in programming?
easy
A. To sort numbers in ascending order automatically
B. To store data in random order for quick access
C. To keep track of function calls and return in the correct order
D. To store large amounts of data permanently
Solution
Step 1: Understand the purpose of a stack
A stack stores items in Last In First Out (LIFO) order, which is perfect for tracking function calls.
Step 2: Relate stack to function calls
When a function calls another, the current function is paused and saved on the stack. When the called function finishes, the program returns to the last saved function.
Final Answer:
To keep track of function calls and return in the correct order -> Option C
Quick Check:
Stack = function call tracking [OK]
Hint: Stack follows LIFO, perfect for tracking nested calls [OK]
Common Mistakes:
Thinking stack stores data randomly
Confusing stack with queue
Assuming stack sorts data automatically
2. Which of the following is the correct way to push an element '5' onto a stack named myStack in TypeScript?
easy
A. myStack.push(5);
B. myStack.add(5);
C. myStack.insert(5);
D. myStack.enqueue(5);
Solution
Step 1: Recall stack method names in TypeScript
Stacks use push() to add elements on top.
Step 2: Identify correct method for adding element
push() is the standard method; others like add(), insert(), or enqueue() are not used for stacks.
Final Answer:
myStack.push(5); -> Option A
Quick Check:
Push adds element on top [OK]
Hint: Use push() to add on stack top [OK]
Common Mistakes:
Using enqueue which is for queues
Using add or insert which are not stack methods
Confusing push with pop
3. What will be the output of the following TypeScript code?