ArrayDeque / Circular Buffer
๐ Quick Reference
| Property | Value |
|---|---|
| Type | Resizable circular array |
| Add/Remove at Ends | O(1) amortized |
| Random Access | Not supported (no get(i)) |
| Memory | O(n) - contiguous, cache-friendly |
| Null Elements | Not allowed |
| Best For | Stack, queue, deque operations |
๐ฎ Interactive Visualizer
Watch how ArrayDeque uses circular indexing to avoid shifting elements:
Loading visualizer...
- Add to front and back - see head/tail pointers move
- Remove from both ends - watch circular wrap-around
- Fill until resize - observe capacity doubling
- Notice: no element shifting needed!
๐ง Key Operations
Creating
JAVA(14 lines)CodeLoading syntax highlighter...
Adding Elements
JAVA(11 lines)CodeLoading syntax highlighter...
Accessing
JAVA(8 lines)CodeLoading syntax highlighter...
Removing
JAVA(14 lines)CodeLoading syntax highlighter...
Iteration
JAVA(8 lines)CodeLoading syntax highlighter...
๐ Complexity Analysis
| Operation | Time | Notes |
|---|---|---|
addFirst/Last | O(1)* | *Amortized - O(n) when resize needed |
removeFirst/Last | O(1) | Just move pointer |
peekFirst/Last | O(1) | Direct access |
remove(Object) | O(n) | Linear search |
contains(Object) | O(n) | Linear search |
size() | O(1) | Stored field |
Circular Buffer Mechanism
Array: [ _, _, C, D, E, F, _, _ ] ^ ^ head tail After addFirst("B"): Array: [ _, B, C, D, E, F, _, _ ] ^ ^ head tail After addLast("G"): Array: [ _, B, C, D, E, F, G, _ ] ^ ^ head tail Wrap-around example: Array: [ G, H, C, D, E, F, _, _ ] ^ ^ tail head (head > tail means wrapped around)
โ When to Use ArrayDeque
Good Use Cases
- Stack implementation - faster than Stack class
- Queue implementation - faster than LinkedList
- Double-ended queue - add/remove both ends
- Sliding window - efficient window operations
- BFS traversal - fast queue for graph algorithms
Avoid When
- Need random access - no get(index) method
- Need null elements - ArrayDeque prohibits null
- Concurrent access - use ConcurrentLinkedDeque
- Need List interface - use LinkedList instead
๐ ArrayDeque vs LinkedList vs Stack
| Feature | ArrayDeque | LinkedList | Stack |
|---|---|---|---|
| Push/Pop | O(1) | O(1) | O(1) |
| Memory | Compact | Node overhead | Legacy |
| Cache | Friendly | Poor | N/A |
| Null | Not allowed | Allowed | Allowed |
| Random Access | No | O(n) | O(n) |
| Recommended | โ Yes | For List ops | โ No |
JAVA(7 lines)CodeLoading syntax highlighter...
๐งฉ Common Patterns
Stack Usage
JAVA(9 lines)CodeLoading syntax highlighter...
Queue Usage
JAVA(9 lines)CodeLoading syntax highlighter...
Sliding Window Maximum
JAVA(24 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Adding Null Elements
JAVA(5 lines)CodeLoading syntax highlighter...
2. Using Wrong Interface Type
JAVA(6 lines)CodeLoading syntax highlighter...
3. Expecting get(index)
JAVA(6 lines)CodeLoading syntax highlighter...
4. Using LinkedList for Stack/Queue
JAVA(5 lines)CodeLoading syntax highlighter...
๐ค Interview Tips
"ArrayDeque uses a contiguous array (better cache locality), has no node allocation overhead, and provides the same O(1) operations. LinkedList has per-node memory overhead and poor cache performance.
"Null is used as a special value to indicate an empty slot in the circular buffer. If null were allowed, you couldn't distinguish between "no element" and "element is null".
"It uses a circular buffer with head and tail indices. Adding/removing just moves these indices (with modulo for wrap-around). No element shifting is needed.
"ArrayDeque maintains insertion order with O(1) access to both ends. PriorityQueue maintains heap order with O(1) access only to minimum. Choose based on whether you need FIFO/LIFO or priority ordering.
๐ Series Navigation
ArrayDeque from @tomaszjarosz/react-visualizers