March 24
Deque
What deque means, when inserting and removing from both ends works better than a simple stack or queue.
What it is
A deque is a structure that lets you insert and remove from both the front and the back.
It sits somewhere between a queue and a stack in flexibility.
When to use it
It appears when the problem needs to:
- operate from both ends
- keep candidates inside a moving window
- combine queue behavior with fast discard
Common mistake
The classic mistake is treating deque as an implementation detail with no algorithmic impact.
In many problems, the ability to work from both ends is exactly what makes the good solution possible.
Better question
Before using it, ask:
- do I need to remove from both the front and the back?
- does a simple queue or stack trap me?
- am I storing candidates that enter and leave from different sides?
Share this page
Copy the link manually from the field below.