In the study of data structures, linear data structures are those where elements are arranged sequentially, one after another. Common examples include arrays, linked lists, stacks, and queues. Each of these structures has specific rules about how elements are added or removed.
A deque (pronounced "deck") stands for double-ended queue. It is a special linear data structure that allows insertion and deletion of elements from both ends - the front and the rear. This flexibility makes deques more versatile than stacks or queues, which restrict operations to one end only.
Why are deques important? Because many real-world problems require this dual-end access. For example, in task scheduling, sometimes you need to add urgent tasks at the front while normal tasks go to the rear. Deques efficiently support such operations without the overhead of shifting elements, making them essential in competitive programming and system design.
A deque is a linear collection of elements that supports insertion and deletion at both its front and rear ends.
Before we dive deeper, let's clarify some terms:
There are two main types of deques based on restrictions on insertion or deletion:
In a general deque, both insertion and deletion are allowed at both ends.
Deques can be implemented primarily in two ways:
Additionally, a circular array implementation is often used to optimize space and avoid shifting elements.
| Aspect | Array-based Deque | Linked List-based Deque |
|---|---|---|
| Memory Usage | Fixed size (static), may waste space if not full | Dynamic size, uses extra memory for pointers |
| Insertion/Deletion Time | O(1) amortized with circular array; otherwise shifting causes O(n) | O(1) guaranteed at both ends |
| Complexity of Implementation | Moderate; requires careful handling of front/rear indices | Relatively simple; pointers manage ends |
| Space Efficiency | May be inefficient if size is overestimated | Efficient; grows as needed |
| Wrap-around Handling | Needed in circular array implementation | Not required |
Let's explore the core operations of a deque. Each operation must handle special cases such as when the deque is empty or full.
graph TD Start[Start Operation] --> CheckFull{Is Deque Full?} CheckFull -- Yes --> Overflow[Overflow Error] CheckFull -- No --> ChooseOp{Insert or Delete?} ChooseOp -- Insert --> ChooseEndInsert{Insert at Front or Rear?} ChooseEndInsert -- Front --> InsertFront[Insert Element at Front] ChooseEndInsert -- Rear --> InsertRear[Insert Element at Rear] ChooseOp -- Delete --> ChooseEndDelete{Delete from Front or Rear?} ChooseEndDelete -- Front --> DeleteFront[Delete Element from Front] ChooseEndDelete -- Rear --> DeleteRear[Delete Element from Rear] InsertFront --> End[End Operation] InsertRear --> End DeleteFront --> CheckEmpty{Is Deque Empty?} DeleteRear --> CheckEmpty CheckEmpty -- Yes --> Underflow[Underflow Error] CheckEmpty -- No --> EndInsertion at Front: Decrement the front pointer and place the new element there.
Insertion at Rear: Increment the rear pointer and place the new element there.
Deletion at Front: Remove the element at the front pointer and increment the front pointer.
Deletion at Rear: Remove the element at the rear pointer and decrement the rear pointer.
In circular array implementations, the front and rear pointers wrap around using modulo arithmetic to stay within array bounds.
Step 1: Insert 10 at rear. Deque: [10]
Step 2: Insert 20 at front. Deque: [20, 10]
Step 3: Insert 30 at rear. Deque: [20, 10, 30]
Step 4: Delete from front removes 20. Deque: [10, 30]
Step 5: Insert 40 at front. Deque: [40, 10, 30]
Step 6: Delete from rear removes 30. Deque: [40, 10]
Answer: Final deque contents are 40 at front and 10 at rear.
Step 1: Insert 5 at rear. front=0, rear=0. Deque: [5]
Step 2: Insert 10 at rear. rear=1. Deque: [5, 10]
Step 3: Insert 15 at rear. rear=2. Deque: [5, 10, 15]
Step 4: Insert 20 at front. front moves from 0 to 4 (wrap-around). Deque: [20, 5, 10, 15]
Step 5: Insert 25 at front. front moves from 4 to 3. Deque: [25, 20, 5, 10, 15]
Step 6: Delete from rear. rear moves from 2 to 1. Deque: [25, 20, 5, 10]
Step 7: Insert 30 at rear. rear moves from 1 to 2. Deque: [25, 20, 5, 10, 30]
Answer: The circular nature allows front and rear to wrap around the array efficiently.
"RADAR", use a deque to check if it is a palindrome. Describe the steps. Step 1: Insert all characters into the deque from left to right: R, A, D, A, R.
Step 2: Compare characters from front and rear:
Answer: The string "RADAR" is a palindrome.
[2, 1, 3, 4, 6, 3, 8, 9, 10, 12, 56] and window size k = 4, find the maximum in each sliding window using a deque. Step 1: Initialize an empty deque to store indices.
Step 2: Process first k elements (0 to 3):
Step 3: For each element from index 4 to end, do:
Step 4: The maximums for windows are: 4, 6, 6, 8, 9, 10, 12, 56.
Answer: Using deque reduces time complexity to O(n) compared to O(nk) in naive methods.
Step 1: Add T1 at rear. Deque: [T1]
Step 2: Add T2 at front. Deque: [T2, T1]
Step 3: Add T3 at rear. Deque: [T2, T1, T3]
Step 4: Process task from front (remove T2). Deque: [T1, T3]
Step 5: Add T4 at front. Deque: [T4, T1, T3]
Step 6: Process two tasks from front (remove T4 and T1). Deque: [T3]
Answer: The deque efficiently manages urgent and normal tasks with front and rear operations.
When to use: When deciding which linear data structure to use for problems requiring flexible insertion/deletion.
When to use: When implementing deques in memory-constrained environments or for performance-critical applications.
When to use: While solving problems involving circular deque implementations.
When to use: When asked to check palindromes efficiently in string processing problems.
When to use: When solving range query problems in arrays or sequences.
| Feature | Deque | Stack | Queue |
|---|---|---|---|
| Insertion | Front & Rear | Top only | Rear only |
| Deletion | Front & Rear | Top only | Front only |
| Flexibility | High | Low | Medium |
| Use Cases | Yes | Function calls, undo operations | Order processing, buffering |
Progress tracking is paywalled — subscribe to mark subtopics as understood and save your streak.
Go to practice →