A Stack is a linear data structure that follows the Last In First Out (LIFO) principle. The element inserted last is removed first. Java provides a Stack class in the java.util package, but using Deque (ArrayDeque) is often preferred for better performance.
Operations:
push(): Add an element to the top.pop(): Remove the top element.peek(): View the top element without removing it.isEmpty(): Check if the stack is empty.
Use-case Analogy: Think of a stack of plates—adding/removing only happens at the top.
Stack Top --> 40 (peek)
30
20
10 <- Bottom
Operations happen only at the top of the stack.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| push | O(1) | O(n) |
| pop | O(1) | O(1) |
| peek | O(1) | O(1) |
| isEmpty | O(1) | O(1) |
Optimization:
- Use
ArrayDequeinstead ofStackclass for faster operations.
-
DFS:
- Use stack to traverse nodes in depth-first order.
-
Expression Parsing:
- Evaluate postfix, prefix, infix expressions.
-
Backtracking:
- Sudoku solver, maze solving use stacks to store paths.
-
Browser Navigation:
- Implement backward/forward using two stacks.
-
Lazy Evaluation in Stacks:
- Delay computation using a
Supplier<T>wrapper untilpop().
- Delay computation using a
-
Two Stacks in One Array:
- Use one array and grow stacks from ends inward.
-
Circular Stack:
- Implement stack using circular buffer for constant time operations.
-
Stack vs Heap:
- Stack memory is for static memory allocation (function calls, primitives).
- Heap memory is for dynamic allocation (objects, arrays).
-
Call Stack Growth:
- Recursive calls add stack frames; large depths can cause stack overflow.
-
Push Multiple Elements:
- Batch
pushAll(Collection<T>)implementation.
- Batch
-
Batch Pop:
- Return last k elements in a list using iteration.
- Create an iterator using an internal list copy for non-destructive traversal.
-
Stack Overflow:
- Use tail-recursion or iterative conversion.
-
Memory Leaks:
- Ensure popped elements are dereferenced to allow garbage collection.
-
Stack and Queue:
- Implement queue using two stacks.
-
Stack and Linked List:
- Use singly linked list to implement a custom stack.
Level 1 (Easy):
- Implement a stack using arrays.
- Reverse a string using stack.
- Balanced parentheses checker.
Level 2 (Medium):
- Design a stack with getMin() in O(1).
- Evaluate postfix expression.
- Next Greater Element problem.
Level 3 (Hard):
- Largest rectangle in histogram.
- Max rectangle of 1s in binary matrix.
- Implement K stacks in an array.
- Explain LIFO clearly with an example.
- Talk through edge cases: empty stack, overflow.
- Describe time/space complexities.
- Avoid explaining too generally—tailor answers to the problem asked.
- Undo functionality in editors.
- Call stack in recursive functions.
- Expression evaluation in compilers.
- Game state tracking.
- Not checking
isEmpty()beforepop(). - Misplacing
pushandpoporder. - Stack overflow due to deep recursion.
- Stack is LIFO.
- Constant time operations.
- Core for recursion, backtracking, expression evaluation.
- Replace
StackwithArrayDequein Java.
Implement an LRU (Least Recently Used) Cache using Stack and LinkedHashMap.
| Stack Type | Usage Scenario | Pros | Cons |
|---|---|---|---|
| Array-based | Fixed size known | Fast access | Fixed capacity |
| Linked list | Dynamic size | No overflow | Extra memory for pointers |
| ArrayDeque | Java alternative to Stack | Better performance | No thread safety |
+-------------------------+
| Method Stack Frame 1 |
| Method Stack Frame 2 |
| ... |
+-------------------------+
- Each function call adds a frame to the stack.
- Stack overflows if too many frames are added (deep recursion).
- Reverse a stack using recursion.
- Design a Min Stack.
- Implement a queue using two stacks.
- Sort a stack.
- Validate stack sequences.
- Decode a nested expression.
- Largest rectangle in histogram.
- Trapping Rain Water using stacks.
- Clone a stack without modifying it.
- Merge two stacks into one.
- LIFO: Last-In-First-Out
- Stack Frame: The memory unit for function calls
- Tail Recursion: A recursion where the recursive call is the last operation
-
Q: Why prefer ArrayDeque over Stack in Java? A: Because Stack is synchronized and slower. ArrayDeque is faster and more memory-efficient.
-
Q: How to avoid stack overflow in recursion? A: Use iterative methods or optimize with tail recursion.
- Always check for edge cases.
- Visualize stack operations to debug.
- Practice problems with increasing complexity.
- Replace recursion with a stack manually to understand control flow.
✅ This topic is foundational. Mastering stacks opens up recursion, tree traversal, and expression parsing.
Click here to continue to the next topic: Queues.