ImplementationEnqueueDequeuePeekSpaceNotes
Library (Deque)Highly optimized. Python uses Linked List blocks; Rust uses Ring Buffer.
Circular QueueStrict . No memory allocation overhead during runtime. Fixed size .
Queue using StacksAmortized Amortized Worst case for a single dequeue is , but average is .

Detailed

1. Library & Circular Queue (Strict )

These implementations track the head and tail pointers/indices.

  • Enqueue: We calculate the index/pointer and insert. No loops. .
  • Dequeue: We calculate the index/pointer and remove. No loops. .
  • Note: Rust’s VecDeque might occasionally resize (grow), making enqueue Amortized , but this is standard for all dynamic arrays.

2. Queue using Stacks (Amortized )

This is the most common follow-up question in interviews.

Why is it Amortized ?

  • Enqueue: We simply push to s1. This is always .
  • Dequeue (Worst Case): If s2 is empty, we must move all items from s1 to s2. This takes .
  • Dequeue (Best Case): If s2 has items, we just pop one. This takes .

The Math (Amortized Analysis): Consider the lifecycle of a single element X:

  1. Pushed onto s1 (Cost: 1)
  2. Popped from s1 (Cost: 1)
  3. Pushed onto s2 (Cost: 1)
  4. Popped from s2 (Cost: 1)

Total operations for X over its entire life = 4 operations. Since 4 is a constant, the average cost per element is .