Stacks & Queues
Stack Operations & Design
Master Stack operations, parenthesis matching, expression evaluations, and classic Stack design patterns like MinStack.
Last Updated: August 2, 2026
•
20 min read
1. Introduction
What are Stack Operations & Design?
Stack Operations are the core API interfaces used to modify stacks:push (insert), pop (delete), peek (view top), and isEmpty. Stack Design refers to composing multiple stacks or lists to implement custom behaviors (like retrieval of the minimum element in constant time).
Why is it Important?
Many computer science validation tasks require tracking nested relationships. Stacks are uniquely suited for parsing nested environments:{[()]}.3 + (4 * 2).Where is it Used?
2. Mental Model: The Bracket Nest
Think of parenthesis matching as nesting dolls:
(, [, {), you open a doll and push it onto your stack.), ], }), it must match the outermost open doll (the top of your stack).3. Core Algorithms & Implementations
1. Valid Parentheses (LeetCode 20)
Using a stack to validate matching open/close bracket pairs.2. MinStack Design (LeetCode 155)
Design a stack that supportspush, pop, top, and retrieving the minimum element in O(1) time.
minStack that stores the running minimum value corresponding to each element in the main stack.4. Visual Trace: Parenthesis Matching
Trace showing validation checks for input "{()}":
5. Real-World Applications
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test nesting and boundary checks:input and output). Push elements onto input. To dequeue, if output is empty, pop all elements from input and push them onto output, then pop from output (O(1) amortized time).Common Mistakes
Warning: 1. Empty Stack Pop Crashes: Attempting to execute
stack.pop() when stack.isEmpty() is true. Always check isEmpty() before popping.> 2. Space overhead of MinStack: Storing duplicates inminStackunnecessarily. Optimize by only pushing ontominStackif the new value is≤the current minimum.
7. Summary
O(1) time.O(1) retrieval.8. Quiz
Question 1: What is the time complexity of the pop operation in a MinStack implemented with a secondary stack?
Answer:O(1) time complexity. We compare values and pop from both stacks in constant time.
Question 2: How do you evaluate a Postfix expression (e.g. '3 4 +') using a stack?
Answer: Scan from left to right. If you see a number, push it onto the stack. If you see an operator, pop the top two numbers, apply the operator, and push the result back. At the end, the stack contains the final evaluated answer.Question 3: In C++, what is the difference between std::stack top() and pop()?
Answer:top() returns a reference to the top element of the stack without removing it. pop() removes the top element but returns void (nothing). In Java, pop() does both: it removes and returns the top element.
Question 4: True or False: If parenthesis matching finishes scanning the string and the stack is not empty, the string is valid.
Answer: False. If the stack is not empty, it means there are unclosed opening brackets (e.g.( ( )), making the string invalid.
Question 5: Can you implement a Stack using a singly linked list? What is the insertion complexity?
Answer: Yes, by inserting and deleting nodes exclusively at the head. Since head updates require no traversals, all stack operations take guaranteedO(1) time.