You are checking whether parentheses in the string '({[]})' are balanced using a stack. When you encounter a closing bracket ']', what is the correct next action?
APush ']' onto the stack so it can be matched later
BPop the top element from the stack and verify it is the matching opening bracket '['
CClear the stack and restart the scan from the current position
DSearch the stack from bottom to top for any '[' and remove it
When a closing bracket is encountered, you pop the top of the stack and check that it matches. This works because the stack always holds the most recently unmatched opening bracket on top — exactly the one that the current closing bracket should close. Option D would be searching a data structure that should only be accessed at its top, which misuses both the stack and the algorithm.
Question 2 Multiple Choice
Function A calls function B, which calls function C. When C finishes and returns, which function resumes and why?
AFunction A resumes, because it initiated the call chain and is the 'root' caller
BFunction B resumes, because its frame was pushed most recently before C's call — LIFO order dictates it is popped first
CFunction A resumes, because the call stack always returns to the original caller after any return
DWhichever function the OS scheduler selects next
The call stack uses LIFO: when C is called, its frame is pushed on top of B's frame. When C returns, its frame is the last one pushed, so it is the first one popped — restoring execution to exactly where B left off. This is not a policy decision; it is the direct result of the stack's LIFO discipline.
Question 3 True / False
A stack's LIFO constraint is a performance limitation compared to arrays, which allow access to any element at any index.
TTrue
FFalse
Answer: False
LIFO is not a limitation — it is the defining feature that makes stacks useful. By restricting access to one end, stacks achieve O(1) push, pop, and peek with zero bookkeeping overhead. Arrays are more general, but generality has a cost: you must track positions manually. For LIFO use cases (call stacks, undo history, expression parsing, DFS), a stack is the right tool and an array with arbitrary access is overkill.
Question 4 True / False
When a recursive function calls itself too many times and the program crashes, this is a direct consequence of the LIFO call stack running out of allocated space.
TTrue
FFalse
Answer: True
Each recursive call pushes a new frame onto the call stack, containing local variables and the return address. The call stack has a fixed size (typically 1–8 MB). Deep recursion without a terminating base case keeps pushing frames until the stack is exhausted — a stack overflow. This is not an abstract concept; it is the real-world manifestation of the underlying stack data structure that every program uses for function calls.
Question 5 Short Answer
Why does LIFO ordering — rather than FIFO or random access — make stacks the natural data structure for tracking the state of recursive function calls?
Think about your answer, then reveal below.
Model answer: Each function call needs to restore the state of the most recent caller when it returns — not the first caller and not a random one. LIFO ensures the most recently pushed frame is always on top and is the first to be popped when that function returns, guaranteeing execution resumes exactly where it was interrupted. Any other access pattern (FIFO, random) would return to the wrong caller or corrupt local variable state.
This is the deeper reason stacks appear everywhere: many real-world processes have a 'last thing entered is the first thing resolved' structure — nested function calls, browser back navigation, undo histories, depth-first graph traversal. The LIFO constraint is not a restriction; it is a precise match for the problem's structure.