Java Notes
Complete guide to Stack data structure — LIFO principle, array and linked list implementations, applications, expression evaluation, and classic problems.
What is a Stack?
A Stack is a LIFO (Last In, First Out) data structure. The last element added is the first one removed — like a stack of plates.
| push(4): [1, 2, 3] | [1, 2, 3, 4] (add to top) |
| pop(): [1, 2, 3, 4] | [1, 2, 3] (remove from top, returns 4) |
| peek(): [1, 2, 3] | returns 3 (view top without removing) |
| All operations | O(1) |
Applications
Balanced Parentheses
Infix to Postfix Conversion
public static String infixToPostfix(String infix) {
StringBuilder result = new StringBuilder();
Stack<Character> stack = new Stack<>();
for (char c : infix.toCharArray()) {
if (Character.isLetterOrDigit(c)) {
result.append(c);
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(')
result.append(stack.pop());
stack.pop(); // Remove '('
} else { // Operator
while (!stack.isEmpty() && precedence(stack.peek()) >= precedence(c))
result.append(stack.pop());
stack.push(c);
}
}
while (!stack.isEmpty()) result.append(stack.pop());
return result.toString();
}
private static int precedence(char op) {
switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; }
return -1;
}Next Greater Element
Min Stack (O(1) getMin)
Interview Questions
Q1: How would you implement a stack using queues?
Answer: Use two queues. For push: add to q1. For pop: move all but last from q1 to q2, remove last from q1, swap q1 and q2. Alternative: single queue, after enqueue rotate n-1 elements.
Q2: How do you sort a stack using only another stack?
Answer: Pop temp from source. While dest has larger elements on top, pop them back to source. Push temp to dest at correct position. Repeat. Time: O(n²).
Q3: What is the difference between Stack class and Deque in Java?
Answer: java.util.Stack extends Vector (synchronized, legacy). Use Deque<Integer> stack = new ArrayDeque<>() instead — faster (no synchronization), preferred per Java docs. Use push()/pop()/peek().
Q4: Design a browser back/forward button.
Answer: Two stacks: backStack and forwardStack. Visit new page: push current to backStack, clear forwardStack. Back: push current to forwardStack, pop backStack. Forward: push current to backStack, pop forwardStack.
Q5: What is the stock span problem?
Answer: For each day, find how many consecutive previous days had price ≤ today. Use a stack storing indices of days with higher prices. For each day, pop smaller elements — span is current index minus stack top index.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Stack in Java — DSA in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, dsa, stack, stack in java — dsa in java
Related Java Master Course Topics