Are there any interesting algorithms using both a stack and queue deque ADT?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Yes, many practical algorithms combine stack and queue behavior, and a deque often provides both efficiently. These combinations are not academic tricks. They solve real ordering problems such as graph traversal, window analytics, and expression processing with predictable performance.
Why Combine These Structures
A stack gives last in first out behavior, which is useful for backtracking and deferred work. A queue gives first in first out behavior, which is useful for level order processing and fairness. A deque supports operations at both ends and can emulate both patterns when needed.
The design benefit is that each operation encodes intent. Push and pop at the right end means local reversal. Append at one end and remove at the other means stable arrival order.
Example 1: Reverse Level Order Traversal
A common pattern uses a queue for breadth first exploration and a stack to reverse output order.
Queue drives exploration breadth wise. Stack reverses the visitation sequence without costly list insertions at the front.
Example 2: Sliding Window Maximum With Monotonic Deque
This algorithm uses a deque to maintain candidates in descending value order. It acts like a queue for window expiry and like a stack for removing weaker candidates.
This pattern is a strong example of dual behavior in one structure. Front removal preserves window boundaries. Back removal preserves monotonic invariant.
Example 3: 0 1 BFS Uses Both Ends Intentionally
For graphs with edge weights only 0 or 1, a deque gives near Dijkstra behavior with simpler operations.
Appending to the front for zero weight edges and to the back for one weight edges preserves optimal processing order.
Choosing The Right Pattern
When deciding between stack, queue, or deque, start from ordering requirements.
- Need strict arrival order, choose queue semantics.
- Need undo or backtracking behavior, choose stack semantics.
- Need both with constant time ends, choose deque.
Then write down the invariant before coding. For sliding window maximum, invariant is descending values by index in the deque. For 0 1 BFS, invariant is shortest tentative distance order induced by edge weight placement.
Without explicit invariants, mixed end operations become hard to review and easy to break.
Common Pitfalls
- Using a Python list as queue and paying linear cost for front removal.
- Mixing traversal intent, such as stack operations in code expected to be breadth first.
- Forgetting to evict out of window indexes in monotonic deque algorithms.
- Not documenting invariants, which makes maintenance risky.
- Assuming deque automatically improves logic without clear operation rules.
Summary
- Many efficient algorithms combine stack and queue behavior by design.
dequeis the practical tool for constant time operations on both ends.- Reverse level traversal, sliding window max, and 0 1 BFS are concrete examples.
- Correctness comes from invariants, not from the container alone.
- Start with ordering requirements, then map each operation to that requirement.
Related reading
- Are there any online algorithms for planarity testing?
- Are there any real Onn algorithms?
- Are there any special image compression algorithms for face cases?
- Are there any worse sorting algorithms than Bogosort a.k.a Monkey Sort?
- Are there O1 random access data structures that don't rely on contiguous storage?
- Are tuples more efficient than lists in Python?
- Arrange array so adjacent has less space that gives minimum sum
- Array maximum difference algorithm that runs in On?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.