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 not just use an array directly instead of a stack?
Using an array directly can cause mistakes because arrays let you add or remove items anywhere, but a stack only allows adding or removing from the top. Stack abstraction keeps this rule safe.
Click to reveal answer
intermediate
What does 'stack abstraction' mean?
Stack abstraction means hiding the details of how data is stored and only allowing actions like push (add) and pop (remove) from the top, so the rules of a stack are always followed.
Click to reveal answer
intermediate
What problems can happen if you use an array directly as a stack without abstraction?
You might accidentally add or remove items from the wrong place, breaking the stack order and causing bugs that are hard to find.
Click to reveal answer
beginner
How does stack abstraction help in programming?
It makes code safer and easier to understand by only allowing stack operations, preventing errors and making the program follow the stack rules clearly.
Click to reveal answer
What order does a stack follow?
ASorted Order
BFirst In, First Out (FIFO)
CLast In, First Out (LIFO)
DRandom Order
✗ Incorrect
A stack follows Last In, First Out (LIFO) order, meaning the last item added is the first to be removed.
Why is using an array directly risky for stack operations?
A. stack should be a global variable inside functions
B. Using insert(0, x) breaks LIFO order
C. pop() should remove from index 0, not end
D. No error, code works correctly
Solution
Step 1: Analyze push() method
push() uses insert(0, x) which adds element at start, reversing order.
Step 2: Analyze pop() method
pop() removes from end, so last element inserted at start is not popped first, breaking LIFO.
Final Answer:
Using insert(0, x) breaks LIFO order -> Option B
Quick Check:
push at start but pop at end breaks stack order [OK]
Hint: Push and pop must use same end for LIFO [OK]
Common Mistakes:
Assuming pop() removes from start by default
Ignoring that insert(0, x) reverses order
Thinking global variable missing causes error here
5. You want to implement a stack that supports retrieving the minimum element in constant time along with push and pop. Why is using a simple array insufficient, and how does stack abstraction help solve this?
hard
A. Array supports min retrieval by default; stack abstraction is unnecessary
B. Array cannot store integers; stack abstraction converts them to strings
C. Stack abstraction slows down operations but uses less memory than arrays
D. Array alone can't track minimum efficiently; stack abstraction allows auxiliary stacks to track min
Solution
Step 1: Understand min retrieval challenge
Retrieving minimum in constant time requires extra tracking beyond simple array storage.
Step 2: Role of stack abstraction
Stack abstraction allows maintaining an auxiliary stack that keeps track of minimums alongside main stack, enabling O(1) min retrieval.
Final Answer:
Array alone can't track minimum efficiently; stack abstraction allows auxiliary stacks to track min -> Option D
Quick Check:
Auxiliary stack for min needs stack abstraction = Array alone can't track minimum efficiently; stack abstraction allows auxiliary stacks to track min [OK]
Hint: Use auxiliary stack with abstraction for constant-time min [OK]
Common Mistakes:
Thinking arrays can track min in O(1) without extra structure