Algorithms

ArrayDeque / Circular Buffer

๐Ÿ“‹ Quick Reference

PropertyValue
TypeResizable circular array
Add/Remove at EndsO(1) amortized
Random AccessNot supported (no get(i))
MemoryO(n) - contiguous, cache-friendly
Null ElementsNot allowed
Best ForStack, queue, deque operations
One-liner: Circular buffer providing O(1) operations at both ends - faster than LinkedList for stack/queue.

๐ŸŽฎ Interactive Visualizer

Watch how ArrayDeque uses circular indexing to avoid shifting elements:

Loading visualizer...
Try these operations:
  1. Add to front and back - see head/tail pointers move
  2. Remove from both ends - watch circular wrap-around
  3. Fill until resize - observe capacity doubling
  4. Notice: no element shifting needed!

๐Ÿ”ง Key Operations

Creating

JAVA(14 lines)
Code
Loading syntax highlighter...

Adding Elements

JAVA(11 lines)
Code
Loading syntax highlighter...

Accessing

JAVA(8 lines)
Code
Loading syntax highlighter...

Removing

JAVA(14 lines)
Code
Loading syntax highlighter...

Iteration

JAVA(8 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Complexity Analysis

OperationTimeNotes
addFirst/LastO(1)**Amortized - O(n) when resize needed
removeFirst/LastO(1)Just move pointer
peekFirst/LastO(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

FeatureArrayDequeLinkedListStack
Push/PopO(1)O(1)O(1)
MemoryCompactNode overheadLegacy
CacheFriendlyPoorN/A
NullNot allowedAllowedAllowed
Random AccessNoO(n)O(n)
Recommendedโœ… YesFor List opsโŒ No
JAVA(7 lines)
Code
Loading syntax highlighter...

๐Ÿงฉ Common Patterns

Stack Usage

JAVA(9 lines)
Code
Loading syntax highlighter...

Queue Usage

JAVA(9 lines)
Code
Loading syntax highlighter...

Sliding Window Maximum

JAVA(24 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Adding Null Elements

JAVA(5 lines)
Code
Loading syntax highlighter...

2. Using Wrong Interface Type

JAVA(6 lines)
Code
Loading syntax highlighter...

3. Expecting get(index)

JAVA(6 lines)
Code
Loading syntax highlighter...

4. Using LinkedList for Stack/Queue

JAVA(5 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Tips

Q: Why prefer ArrayDeque over LinkedList for stack/queue?
"

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.

Q: Why can't ArrayDeque contain null?
"

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".

Q: How does ArrayDeque achieve O(1) at both ends?
"

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.

Q: ArrayDeque vs PriorityQueue?
"

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


Visualizer: ArrayDeque from @tomaszjarosz/react-visualizers