Skip to content
Home Interview Stack and Queue Interview Problems: Patterns That Actually Get You Hired

Stack and Queue Interview Problems: Patterns That Actually Get You Hired

Where developers are forged. · Structured learning · Free forever.
📍 Part of: Coding Patterns → Topic 10 of 17
Master stack and queue interview problems with real coding patterns, battle-tested Java solutions, and the exact techniques interviewers are looking for in 2024.
⚙️ Intermediate — basic Interview knowledge assumed
In this tutorial, you'll learn
Master stack and queue interview problems with real coding patterns, battle-tested Java solutions, and the exact techniques interviewers are looking for in 2024.
  • Bracket/parenthesis matching is always a stack problem — the LIFO property mirrors the reversal of nesting. If a problem involves 'the most recently unmatched thing', reach for a stack immediately.
  • Monotonic stacks solve 'next greater/smaller element' problems in O(n) instead of O(n²) — the pattern is: maintain a stack of unresolved indices and resolve them when the monotonic property is broken.
  • BFS requires a queue — not because of convention but because FIFO is what preserves the 'shortest path' and 'level by level' properties. Swapping it for a stack turns BFS into DFS with no shortest-path guarantee.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
Quick Answer

Imagine a stack of pancakes — you can only add or remove from the top. That's a Stack: last in, first out. Now imagine a line at a theme park — the first person in line is the first to get on the ride. That's a Queue: first in, first out. Most interview problems that feel impossible become obvious once you realize 'wait, this is just a pancake stack situation' or 'this is just a queue of customers waiting their turn'.

Stack and queue problems show up in almost every serious technical interview — from FAANG to mid-size startups — and they're deceptively tricky. On the surface they look like simple data structures. Under the hood, they're the backbone of browser history, undo/redo systems, CPU scheduling, BFS/DFS graph traversal, and expression parsing. Interviewers love them because a candidate who really understands stacks and queues thinks recursively and iteratively at the same time.

The real problem isn't that these structures are hard to understand — it's that most developers memorize their APIs without building intuition for WHEN to reach for one. You see a problem involving parentheses validation, and your brain needs to automatically light up with 'stack'. You see shortest-path or level-order traversal and it should scream 'queue'. That automatic pattern recognition is exactly what this article builds.

By the end of this article you'll be able to recognize the five core problem patterns that map to stacks and queues, implement clean solutions with proper edge-case handling, and articulate your reasoning out loud — which is exactly what interviewers are grading you on. Let's build that intuition from the ground up.

Pattern 1 — Matching and Nesting: When Stacks Are the Only Logical Choice

The single most common stack interview problem is bracket/parenthesis validation, and its siblings: expression evaluation, HTML tag matching, and nested structure parsing. The underlying pattern is always the same: you encounter an 'opening' token and you need to remember it until you see its matching 'closing' token.

Why does a stack work here and nothing else? Because nesting is LIFO by nature. The most recently opened bracket must be the first one closed. Think of Russian dolls — you open the outermost, then the middle, then the smallest. You close them in exact reverse order. A stack mirrors that reversal automatically.

The algorithm is clean: scan left to right, push every opening character, and when you hit a closing character, peek at the stack's top. If it matches, pop and continue. If it doesn't match, or the stack is empty when you expected a match, the structure is invalid. After scanning everything, the stack must be empty — a non-empty stack means unclosed brackets.

This pattern generalizes beyond brackets. Any problem where you need to 'remember the most recent unmatched thing' is a stack problem. Undo/redo in text editors works exactly this way.

BracketValidator.java · JAVA
123456789101112131415161718192021222324252627282930313233343536
package io.thecodeforge.patterns;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;

public class BracketValidator {

    private static final Map<Character, Character> MATCHING_OPEN = Map.of(
        ')', '(',
        ']', '[',
        '}', '{'
    );

    public static boolean isValid(String expression) {
        if (expression == null) return false;
        
        Deque<Character> stack = new ArrayDeque<>();

        for (char token : expression.toCharArray()) {
            if (MATCHING_OPEN.containsValue(token)) {
                stack.push(token);
            } else if (MATCHING_OPEN.containsKey(token)) {
                if (stack.isEmpty() || stack.pop() != MATCHING_OPEN.get(token)) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println("Validating '({[]})': " + isValid("({[]})"));
        System.out.println("Validating '([)]': " + isValid("([)]"));
    }
}
▶ Output
Validating '({[]})': true
Validating '([)]': false
💡Interview Gold:
Never use Java's legacy Stack class in interviews — use ArrayDeque<> instead. It's faster (no synchronized overhead), more memory-efficient, and the Deque interface makes your intent clearer. Saying this out loud in an interview signals you know idiomatic modern Java.

Pattern 2 — Monotonic Stacks: Solving 'Next Greater Element' Problems in O(n)

This is the pattern that separates candidates who've just memorized data structures from those who truly understand them. A monotonic stack maintains elements in either strictly increasing or strictly decreasing order as you push. Whenever a new element breaks that order, you pop — and each pop represents a discovered answer.

The classic problem: given an array of daily temperatures, find how many days you have to wait until a warmer day. The brute-force approach is O(n²) — for each day, scan forward. The monotonic stack approach is O(n) — a single pass.

Here's the key insight: you maintain a stack of indices where you haven't yet found a warmer day. When you reach a temperature that's warmer than what's on top of the stack, that day IS the answer for whatever is being popped. You pop, record the wait, and keep checking.

This pattern generalizes to: Next Greater Element, Next Smaller Element, Largest Rectangle in Histogram, Trapping Rain Water, and Stock Span problems. If you see 'find the next X in an array', think monotonic stack immediately. The direction of monotonicity (increasing vs decreasing) depends on whether you're hunting for 'greater' or 'smaller' — but the mechanism is identical.

DailyTemperatures.java · JAVA
12345678910111213141516171819202122232425262728
package io.thecodeforge.patterns;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class DailyTemperatures {

    public static int[] daysUntilWarmer(int[] temperatures) {
        int n = temperatures.length;
        int[] result = new int[n];
        Deque<Integer> stack = new ArrayDeque<>();

        for (int i = 0; i < n; i++) {
            while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                int prevIndex = stack.pop();
                result[prevIndex] = i - prevIndex;
            }
            stack.push(i);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] temps = {73, 74, 75, 71, 69, 72, 76, 73};
        System.out.println("Wait days: " + Arrays.toString(daysUntilWarmer(temps)));
    }
}
▶ Output
Wait days: [1, 1, 4, 2, 1, 1, 0, 0]
🔥The Pattern Rule:
Monotonic decreasing stack = 'next greater element' problems. Monotonic increasing stack = 'next smaller element' problems. Flip the comparison operator, and the same skeleton solves the opposite problem. Memorize the skeleton, not the specific problem.

Pattern 3 — BFS with Queues: Level-Order Traversal and Shortest Path

If stacks are about 'reversing' and 'remembering the most recent thing', queues are about 'fairness' and 'processing in order'. Breadth-First Search (BFS) is the canonical queue problem — and it comes up disguised in dozens of interview questions: level-order tree traversal, shortest path in an unweighted graph, rotting oranges, word ladder, and island counting.

Why must BFS use a queue and not a stack? Because BFS explores layer by layer — all nodes at distance 1 before distance 2 before distance 3. A queue's FIFO guarantee is what preserves that layer ordering. A stack would give you DFS, which dives deep before wide and loses the 'shortest path' property.

The BFS skeleton is worth committing to memory: enqueue the starting node, then loop while the queue isn't empty — dequeue, process, and enqueue all unvisited neighbors. The moment you enqueue a neighbor, mark it visited to prevent re-processing (this is the single most common BFS bug).

For level-order traversal specifically, you need to know exactly which nodes belong to the same level. The trick: check the queue's size at the start of each level iteration. That size tells you exactly how many nodes to process before starting the next level.

BinaryTreeLevelOrder.java · JAVA
1234567891011121314151617181920212223242526272829303132
package io.thecodeforge.patterns;

import java.util.*;

public class BinaryTreeLevelOrder {
    static class TreeNode {
        int val; TreeNode left; TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) return result;

        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int levelSize = queue.size();
            List<Integer> currentLevel = new ArrayList<>();
            
            for (int i = 0; i < levelSize; i++) {
                TreeNode node = queue.poll();
                currentLevel.add(node.val);
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            result.add(currentLevel);
        }
        return result;
    }
}
▶ Output
Level-order traversal: [[3], [9, 20], [15, 7]]
⚠ Watch Out:
The most common BFS bug is marking a node visited AFTER you dequeue it, instead of WHEN you enqueue it. This causes the same node to be added to the queue multiple times, leading to either wrong answers or infinite loops on graphs with cycles. Always mark visited at enqueue time, not dequeue time.

Pattern 4 — Designing Stack/Queue Hybrids: The 'Implement X Using Y' Problem

A favourite interview question type is 'implement a Queue using two Stacks' or 'implement a Stack using two Queues'. These problems aren't about memorizing the solution — they're about demonstrating that you deeply understand the difference between LIFO and FIFO and can mechanically transform one into the other.

For Queue using two Stacks: one stack is your inbox (push stack), one is your outbox (pop stack). You always push to the inbox. When you need to dequeue (FIFO order), if the outbox is empty, you pour everything from the inbox into the outbox — this reversal converts LIFO into FIFO. If the outbox isn't empty, just pop from it. The amortized time complexity is O(1) per operation, which is the key insight interviewers want you to articulate.

Why does this matter beyond interviews? This pattern is used in real producer-consumer systems. A background thread pushes to the inbox stack without contention. The consumer thread drains the outbox, only acquiring a lock when it needs to refill. It reduces lock contention dramatically compared to a single shared queue.

Also know the 'Min Stack' variant — a stack that supports push, pop, and getMin() all in O(1). The trick: maintain a parallel 'minimums' stack that only pushes when the new element is less than or equal to the current min.

QueueUsingTwoStacks.java · JAVA
1234567891011121314151617181920212223242526
package io.thecodeforge.patterns;

import java.util.ArrayDeque;
import java.util.Deque;

public class QueueUsingTwoStacks {
    private Deque<Integer> inbox = new ArrayDeque<>();
    private Deque<Integer> outbox = new ArrayDeque<>();

    public void enqueue(int x) {
        inbox.push(x);
    }

    public int dequeue() {
        if (outbox.isEmpty()) {
            while (!inbox.isEmpty()) outbox.push(inbox.pop());
        }
        return outbox.pop();
    }

    public static void main(String[] args) {
        QueueUsingTwoStacks q = new QueueUsingTwoStacks();
        q.enqueue(1); q.enqueue(2);
        System.out.println("Dequeued: " + q.dequeue()); // Should be 1
    }
}
▶ Output
Dequeued: 1
💡Pro Tip:
When asked this question in an interview, proactively mention amortized O(1) time complexity. Explain that each element crosses from inbox to outbox exactly once over its lifetime — so even though a single dequeue might trigger an O(n) refill, the total cost spread across n operations is O(1) per operation. This level of analysis is what gets you the senior engineer nod.

Pattern 5 — Monotonic Deque: Sliding Window Maximum

When you combine the power of a sliding window with a monotonic structure, you get the Monotonic Deque. This is the optimal way to find the maximum or minimum element in every sliding window of size K as it moves across an array. While a PriorityQueue would give you O(n log k), a Monotonic Deque provides O(n).

SlidingWindowMax.java · JAVA
1234567891011121314151617181920212223242526272829303132333435363738
package io.thecodeforge.patterns;

import java.util.*;

public class SlidingWindowMax {
    /**
     * Returns the maximum for each sliding window of size k.
     * Uses a deque to store indices of elements in decreasing order.
     */
    public static int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length == 0) return new int[0];
        int n = nums.length;
        int[] result = new int[n - k + 1];
        Deque<Integer> deque = new ArrayDeque<>(); // stores indices

        for (int i = 0; i < n; i++) {
            // 1. Remove indices out of window range
            if (!deque.isEmpty() && deque.peekFirst() == i - k) {
                deque.pollFirst();
            }
            // 2. Remove smaller elements from the back (maintaining monotonic decrease)
            while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
                deque.pollLast();
            }
            deque.offerLast(i);
            // 3. Add to result once window reaches size k
            if (i >= k - 1) {
                result[i - k + 1] = nums[deque.peekFirst()];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {1, 3, -1, -3, 5, 3, 6, 7};
        System.out.println("Window Maxes: " + Arrays.toString(maxSlidingWindow(nums, 3)));
    }
}
▶ Output
Window Maxes: [3, 3, 5, 5, 6, 7]
🔥Complexity Deep Dive:
The deque approach is O(n) because each element is added to the deque once and removed at most once. This outperforms the O(n log k) heap solution, making it the preferred 'Senior' answer.
AspectStack (LIFO)Queue (FIFO)
Core principleLast In, First Out — most recent item processed firstFirst In, First Out — oldest item processed first
Real-world analogyStack of plates, browser Back button, undo historyTicket line, print spooler, BFS exploration
Java interface to useDeque<T> via ArrayDeque (push/pop/peek)Queue<T> via ArrayDeque (offer/poll/peek)
Primary interview patternsBracket matching, monotonic stack, expression evalBFS traversal, level-order, shortest unweighted path
Traversal typeDepth-First Search (DFS) — iterative versionBreadth-First Search (BFS)
Add elementpush() — always to the topoffer() — always to the back
Remove elementpop() — always from the toppoll() — always from the front
When to choose itNeed reversal, nesting, or 'most recent' logicNeed order preservation or layer-by-layer processing
Worst-case spaceO(n) — entire input can land on stack (e.g. all opens)O(n) — entire level can sit in queue at once (wide trees)

🎯 Key Takeaways

  • Bracket/parenthesis matching is always a stack problem — the LIFO property mirrors the reversal of nesting. If a problem involves 'the most recently unmatched thing', reach for a stack immediately.
  • Monotonic stacks solve 'next greater/smaller element' problems in O(n) instead of O(n²) — the pattern is: maintain a stack of unresolved indices and resolve them when the monotonic property is broken.
  • BFS requires a queue — not because of convention but because FIFO is what preserves the 'shortest path' and 'level by level' properties. Swapping it for a stack turns BFS into DFS with no shortest-path guarantee.
  • Always use ArrayDeque over Java's Stack class and always mark BFS nodes visited at enqueue time, not dequeue time — these two habits alone eliminate the most common implementation bugs in interviews.

⚠ Common Mistakes to Avoid

    Using Java's Stack class instead of ArrayDeque — Stack extends Vector, which is synchronized on every operation. In interviews and in production this adds unnecessary overhead. Fix: always declare Deque<Integer> stack = new ArrayDeque<>() and use push()/pop()/peek(). It's faster, not thread-safe (which is fine when you control the single thread), and follows modern Java idioms.
    Fix

    always declare Deque<Integer> stack = new ArrayDeque<>() and use push()/pop()/peek(). It's faster, not thread-safe (which is fine when you control the single thread), and follows modern Java idioms.

    Marking BFS nodes visited at dequeue time instead of enqueue time
    Symptom

    nodes appear in results multiple times, or the algorithm runs into an infinite loop on graphs with back-edges.

    Fix

    the instant you add a neighbor to the queue, also add it to your visited set. This prevents a node from being enqueued a second time before it ever gets processed.

    Forgetting to handle the empty stack/queue before calling pop() or poll()
    Symptom

    NoSuchElementException or NullPointerException mid-algorithm, typically on edge-case inputs like a single character or an empty string.

    Fix

    always guard with isEmpty() checks before popping, and explicitly test your solution against empty input, single-element input, and already-invalid input before calling it done.

Interview Questions on This Topic

  • QValid Parentheses: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. (LeetCode #20)
  • QMin Stack: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. (LeetCode #155)
  • QBinary Tree Level Order Traversal: Given the root of a binary tree, return the level order traversal of its nodes' values. (LeetCode #102)
  • QTrapping Rain Water: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. How does a monotonic stack optimize this? (LeetCode #42)
  • QRotting Oranges: You are given an m x n grid where each cell can have an empty, fresh, or rotten orange. Determine the minimum time until no fresh orange remains using BFS. (LeetCode #994)

Frequently Asked Questions

Why is Deque preferred over Stack in Java?

The legacy Stack class is based on Vector, making it synchronized and thread-safe. This synchronization is almost never needed in coding interviews and adds significant overhead. Deque (via ArrayDeque) is a more efficient, non-synchronized implementation that is faster for single-threaded stack operations.

How do you solve 'Shortest Path in a Grid' using a Queue?

This is a classic Breadth-First Search (BFS) application. By using a queue to explore neighbors layer-by-layer, the first time you reach the destination, you are guaranteed to have taken the shortest path. Ensure you use a 'visited' set to avoid processing the same coordinate twice.

What happens if I use a Stack for BFS?

If you swap the queue for a stack in a BFS algorithm, it effectively becomes Depth-First Search (DFS). While you will still visit all reachable nodes, you lose the 'shortest path' property, as the algorithm will dive deep into one branch before exploring closer neighbors.

What is the space complexity of a recursive stack?

The space complexity of recursion is proportional to the maximum depth of the call stack (O(depth)). In many tree problems, this is O(h) where h is the height. However, in the worst case (a skewed tree), this can be O(n). This is why interviewers often ask for iterative solutions using an explicit Stack to avoid StackOverflowErrors.

🔥
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousBit Manipulation Interview ProblemsNext →Prefix Sum Interview Problems
Forged with 🔥 at TheCodeForge.io — Where Developers Are Forged