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.
Stack and queue practice questions
Implement a stack with a get_max method that returns the largest item in O(1) time: the max stack problem, with a step-by-step solution. keep reading »
Implement A Queue With Two Stacks »Implement a queue with two stacks. Assume you already have a stack implementation. keep reading »
Parenthesis Matching »Write a function that finds the corresponding closing parenthesis given the position of an opening parenthesis in a string. keep reading »
Bracket Validator »Check whether a string's brackets, braces, and parentheses are properly nested and closed: the valid parentheses problem, solved step by step in O(n) time. keep reading »
Running Median »Track the median of a stream of numbers with O(log n) inserts and O(1) lookups, using a max-heap and a min-heap. Step-by-step solution. keep reading »
Keep going with the Queues and stacks section of our coding interview course.
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?
How do you implement a queue using two stacks?
How do you check whether brackets are balanced?
When should I use a stack in an interview problem?
What is a monotonic stack?
More interview questions by topic
- Array and string interview questions
- Hash table interview questions
- Greedy algorithm interview questions
- Sorting and binary search interview questions
- Binary tree and BST interview questions
- Graph interview questions
- Dynamic programming and recursion interview questions
- Linked list interview questions
- Bit manipulation interview questions
- System design interview questions
- All coding interview questions
Interview Cake