LESSON 38 / 75·2 min readIntermediate

Stack and queue in Java

Should knowConcept lesson
01 Understand02 Recall03 Prepare

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

PAUSE & RECALL

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

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Recursion and backtracking in Java

Adapted from 15_DSA_JAVA.md · “5. Stack and queue in Java”. Examples are study fragments; framework behavior can depend on your version.