Queues, Deques & Circular Buffers
Master First-In-First-Out architectures, implement queues using two stacks, and leverage double-ended queues for sliding window optimizations.
1. FIFO Mechanics & Implementation Strategies
A queue follows a First-In-First-Out (FIFO) access pattern where elements are inserted at the back (enqueue) and removed from the front (dequeue). While simple array-based implementations suffer from linear time removal shifts when popping from index zero, circular buffers and linked-list structures achieve true $O(1)$ constant-time operations.
Circular Buffers: Fixed-size arrays that wrap around using modulo arithmetic (`(head + 1) % capacity`), avoiding memory reallocation overhead and ensuring high cache efficiency in stream processing.
2. Queue Implementation Using Two Stacks
You can simulate a queue using two stacks: an `input` stack for incoming pushes and an `output` stack for pops and peeks. When the output stack is empty, elements from the input stack are transferred over, reversing their order and restoring correct FIFO behavior with amortized $O(1)$ complexity.
3. Sliding Window Maximum Using Deque
A double-ended queue (deque) allows efficient insertion and deletion at both ends in $O(1)$ time. By maintaining a monotonically decreasing deque of indices, you can solve sliding window maximum problems in linear $O(n)$ time by trimming out-of-bound elements and smaller redundant values.
4. Homework & Quiz Overview
To lock in your mastery, complete the implementation exercises for building a queue using two stacks and designing a memory-efficient circular queue.
Quick Check: Test Your Intuition
What is the amortized time complexity of the `pop` or `peek` operation in a queue implemented using two stacks?