Intermediate·Concept·2 min read

Stack and queue in Java

Practice this question

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

java
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();

Adapted from 15_DSA_JAVA.md · “5. Stack and queue in Java”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.