16 / 18

How do you implement a Queue using two Stacks?

Difficulty: 4/10
Stacks and Queues, Amortized Analysis, Concurrency

Queue Using Two Stacks

A Queue can be implemented using two Stacks by using one stack for incoming elements and another for outgoing elements. When Dequeue is requested and the output stack is empty, elements are transferred from the input stack to reverse their order, producing FIFO behavior.

javascript
  1. 1

    Enqueue is O(1).

  2. 2

    A single Dequeue can be O(n) when transfer is required.

  3. 3

    Dequeue is amortized O(1) because each element moves from input to output at most once.

  4. 4

    Auxiliary space is O(n).

  5. 5

    This is a classic example of amortized analysis.

Scenario Questions

0-2 years experience

  1. 1We are building a simple undo/redo history queue using two stack-based buffers. If we enqueue 'A', 'B', and 'C', and then perform two dequeues, walk me through exactly how the elements move between your two stacks and what the state of each stack is at each step.
  2. 2Imagine you've implemented this two-stack queue, but your users are reporting that the very first dequeue operation after a long series of enqueues is causing a noticeable UI stutter. Why is this happening, and how would you explain the time complexity of that specific dequeue versus subsequent ones?

2-5 years experience

  1. 1We have a legacy system where we only have access to a third-party Stack library, and we need to implement a thread-safe message queue. If multiple worker threads are calling enqueue and dequeue simultaneously, where do the race conditions occur in the two-stack transfer logic, and how would you synchronize them?
  2. 2Suppose we are implementing a rate-limiter queue using this two-stack approach. If the queue reaches its maximum capacity, we need to reject new enqueues. How do you track and enforce a strict capacity limit across two separate stacks without introducing a bottleneck on every push and pop?

5-8 years experience

  1. 1We are designing an in-memory event broker component that uses a two-stack queue to buffer incoming telemetry. Under heavy write-load, the transfer from Stack A to Stack B causes latency spikes that violate our SLA. How would you redesign the transfer mechanism—perhaps using incremental copying or a ring-buffer hybrid—to eliminate these latency spikes?
  2. 2In a low-latency trading system, we are using two lock-free stacks to implement a lock-free queue. How do you handle the memory reclamation problem (like the ABA problem) when elements are being popped from Stack A and pushed to Stack B by concurrent threads?

8+ years experience

  1. 1We are migrating a high-throughput ingestion pipeline from an old architecture that relied on a custom two-stack persistent queue (backed by disk) to a modern distributed queue like Kafka. How do you design a zero-downtime migration strategy that guarantees message ordering and prevents duplication while draining the legacy two-stack queue?
  2. 2Your team is debating whether to build a custom, highly-optimized two-stack queue variant to handle a specific priority-routing use case, versus adopting an off-the-shelf queue service. How do you evaluate the long-term maintenance overhead, hardware utilization costs, and developer velocity tradeoffs of these two paths?

Follow-up Questions

  • What is the amortized time complexity of the dequeue operation, and how do you prove it?
  • How would you handle concurrent enqueue and dequeue operations without locking the entire data structure?
  • If Stack A is on a local disk and Stack B is in memory, how does that change your transfer strategy?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.