Stack and queue in Java
Stack
Use Deque<Integer> stack = new ArrayDeque<>();
Queue
Use Queue<Integer> queue = new LinkedList<>();
Problems to practice
- Valid Parentheses
- Min Stack
- Daily Temperatures
- Largest Rectangle in Histogram
- Sliding Window Maximum
- Implement Queue using Stacks
Example: stack-based validation
Deque<Character> stack = new ArrayDeque<>();
for (char ch : s.toCharArray()) {
if (ch == '(' || ch == '{' || ch == '[') {
stack.push(ch);
} else if (!stack.isEmpty() && isMatching(stack.peek(), ch)) {
stack.pop();
} else {
return false;
}
}
return stack.isEmpty();Can you explain it without the notes?
Stack and queue in Java
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Stack
Turn this concept into a clear answer.
Practice this conceptAdapted from 15_DSA_JAVA.md · “5. Stack and queue in Java”. Examples are study fragments; framework behavior can depend on your version.