Stack and Queue Interview Questions

How to approach stack and queue questions

Stack and queue questions come in two kinds. Some ask you to build one, like a queue made from two stacks, or a stack that can also return its largest item in O(1) time. Others hide a stack or a queue inside the problem, like checking whether brackets are balanced or handling items in the order they arrived.

Reach for a stack when the most recent item is the one you need next: matching a closing bracket with its opening one, undoing the last action, or replacing recursion with a loop. Reach for a queue when items should be handled in the order they arrived, as in breadth-first search or scheduling tasks.

For design questions, say what each operation costs. Push and pop on a stack, and enqueue and dequeue on a queue, should all be O(1). When some calls do more work than others, give the amortized cost, which is often O(1).

Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.

Concepts to review

  • Stacks: Last in, first out: push and pop at the top in O(1) time.
  • Queues: First in, first out: add at the back and remove from the front in O(1) time.
  • The call stack: The stack your program uses for function calls, and why very deep recursion overflows it.
  • Priority queues: A queue that always hands back the highest-priority item first, usually built on a heap.
  • Amortized analysis: Why an occasional slow operation can still average out to O(1).
  • Breadth-first search: The classic algorithm that runs on a queue.

Quick reference: Data structures cheat sheet

Stack and queue interview FAQ

What's the difference between a stack and a queue?

A stack is last in, first out (LIFO): you remove the item that was added most recently. A queue is first in, first out (FIFO): you remove the item that has been waiting longest. Both add and remove items in O(1) time.

How do you implement a queue using two stacks?

Push new items onto an "in" stack. To dequeue, pop from an "out" stack, but if it's empty, first pop every item off the in stack and push it onto the out stack, which reverses their order. Each item moves between the stacks at most once, so enqueue and dequeue take amortized O(1) time.

How do you check whether brackets are balanced?

Scan the string from left to right and push each opening bracket onto a stack. At each closing bracket, the top of the stack must be the matching opener, so pop it; if the stack is empty or the opener doesn't match, the brackets aren't balanced. At the end, the stack must be empty. This takes O(n) time.

When should I use a stack in an interview problem?

When you need to handle the most recent item first: matching nested brackets or tags, undoing actions, evaluating expressions, or walking a tree or graph depth-first without recursion. A problem that involves nesting or backtracking is a good hint.

What is a monotonic stack?

A stack whose items are kept in increasing (or decreasing) order: before pushing a new item, you pop every item that would break the order. It answers questions like "what's the next larger number to the right of each item?" in O(n) time, since each item is pushed and popped at most once.
. . .